Notes
The list
Here is the list. Jumping straight to code. One god class. Patterns used for show. Ignoring concurrency. A design with no room to extend. Cryptic names and money stored as a double. No demo or test. Silent failures and null returns. And the opposite trap, over-engineering. We will take them one at a time, each with a concrete fix.
- 1 jumping straight to code
- 2 one god class
- 3 patterns for show
- 4 ignoring concurrency
- 5 no room to extend
- 6 cryptic names, double money
- 7 no demo or test
- 8 silent failures, nulls
- 9 over-engineering
1 · Jumping to code
Mistake one is opening the editor in minute two. You end up designing in your head, alone and in silence, and you often build the wrong thing. The fix is the method from the last lesson: five minutes on requirements and scope, then entities, a quick diagram and the main flows, and only then code. A wrong assumption found at minute four costs one sentence. Found at minute forty, it costs the round.
- minutes 0-5: requirements
- and scope, out loud
- minutes 5-20: entities,
- diagram, flows
- then code, with agreement
- a wrong guess found at
- minute 4 costs a sentence;
- at minute 40 it costs
- the round
2 · God class
Here is the before code, a class called OrdMgr. One method works out the delivery fee, applies a discount, saves the order into a static list and sends a text message. It has four reasons to change, so every change risks breaking the other three. You cannot test the price without also sending a message. This is a god class, and interviewers spot it in seconds.
double calc(String t, double amt, int km) {
double f;
if (t.equals("std")) f = 30 + km * 5;
else if (t.equals("exp")) f = 60 + km * 8;
else f = 0;
double tot = amt + f;
tot = tot - tot * 0.1;
lst.add(t + ":" + tot);
System.out.println("SMS: " + tot);
return tot;
} 2 · Fix: split it
The fix is to give each job its own home. A DeliveryFeePolicy works out fees. Money handles amounts and discounts. An OrderStore saves orders. An OrderListener reacts when an order is placed, for example by sending a text. CheckoutService just coordinates them in the right order. Each class now has one reason to change, and each can be tested or replaced alone.
3 · Patterns for show
Mistake three is naming patterns to impress. A factory, a builder and a singleton, before any need exists. Interviewers read this as memorised, not designed. Flip the order: start from a pain, then name the pattern that removes it. Fees change by city and delivery speed, so the fee is a Strategy. A simple test: can you finish the sentence, I used this because, with a real requirement?
- bad: "I will add a Factory,
- a Builder and a Singleton"
- good: "fees change by city,
- so the fee is a Strategy"
- test: can you finish
- "I used X because ..."
- with a real requirement?
4 · Ignoring concurrency
Mistake four is ignoring threads. The old class adds to a static array list shared by every request. Two checkouts at once can corrupt it or lose an order, and ids can repeat. The fix: name the shared state and protect it. Orders go into a ConcurrentHashMap, ids come from an AtomicLong, and data passed between classes is an immutable record. Bring this up before the interviewer asks; it is one of the strongest signals you can give.
- before: static List lst =
- new ArrayList() // shared
- after:
- • ConcurrentHashMap for orders
- • AtomicLong for order ids
- • immutable records passed
- between classes
- say it before they ask
5 · No room to extend
Mistake five is a design that cannot grow. In the old code, a new delivery type means another else if inside the god method, and a typo like express instead of exp silently makes delivery free. The fix: put the varying rule behind an interface. Standard and express are classes implementing DeliveryFeePolicy. Adding scheduled delivery is a new class, and a misspelt type no longer compiles.
interface DeliveryFeePolicy {
Money fee(int km);
}
final class ExpressDelivery
implements DeliveryFeePolicy {
public Money fee(int km) {
return Money.rupees(60)
.plus(Money.rupees(8 * km));
}
} 6 · Names and types
Mistake six is names nobody can read and the wrong types. OrdMgr, calc, t, f, lst and tot force the interviewer to decode your code. Use full words: CheckoutService, place, delivery, fee. Money as a double gives rounding errors, so wrap it in a Money record holding whole paise in a long. Replace magic strings with enums or real types. Good names are free, and they make your design easy to follow.
- OrdMgr, calc, t, f, lst, tot
- -> CheckoutService, place,
- delivery, fee, orders,
- total
- double money -> Money record
- in long paise
- "std" / "exp" -> types the
- compiler checks
7 · No demo or test
Mistake seven is never running the code. If you stop at the last class, the interviewer has to trust that it works. The fix costs five minutes: a main method with a few named checks that print pass or fail. Check the fee for each delivery type, the discount, an edge case, and one concurrent run. Bugs you catch yourself count in your favour, and bugs they catch count against you.
static void check(String name,
boolean ok) {
System.out.println(
(ok ? "PASS " : "FAIL ") + name);
}
check("express fee", ...);
check("10% discount", ...);
check("100 parallel orders", ...); 8 · Silent failures
Mistake eight is failing silently. The old code treats an unknown delivery type as free, and lookups return null that crashes three calls later. The fix is to fail loudly and early. Validate input where it enters, use Optional when something may not exist, and throw exceptions with a message that says what went wrong. A clear error is part of a good design, not an afterthought.
- before: unknown type ->
- fee 0, nobody notices
- before: return null when
- not found
- after:
- • validate input at the edge
- • Optional for "not found"
- • exceptions with a message
- that says what to fix
9 · Over-engineering
Mistake nine is the opposite of the god class: too many layers. Interfaces with one implementation and no likely second one, factories that build factories, a generic framework for a forty five minute problem. You run out of time and the design becomes hard to explain. Add seams only where change is likely, like fees and notifications, and keep the rest plain and direct.
- signs:
- • an interface with one
- implementation and no
- likely second one
- • factories of factories
- • generic frameworks for
- a 45-minute problem
- add seams where change is
- likely; keep the rest plain
After: the flow
Here is the refactored flow for one order. The checkout service asks the delivery policy for the fee for three kilometres: forty five rupees. Money adds it to the item total and takes ten percent off, rounding to whole paise. The store saves the order with a fresh id, and the listener is told the order was placed, which is where the text message goes. Each step lives in the class that owns it.
After: the code
And the after code. The place method reads almost like the requirement: total equals the items plus the delivery fee, minus the discount. Save it, tell the listener, return the order. No string checks, no doubles, no shared array list, no hidden text messages. The class is short enough to explain in thirty seconds, which is exactly what you want in an interview.
PlacedOrder place(OrderRequest r) {
Money total = r.itemsTotal()
.plus(r.delivery().fee(r.km()))
.percentOff(discountPercent);
PlacedOrder order = store.save(
r.customer(), total);
listener.placed(order);
return order;
} A last-5-minutes check
Before you say you are done, run a quick self review. Are the requirements written down? Does any class do three or more jobs? Does every pattern have a reason? Is shared state protected? Is a new type just a new class? Are the names readable and money safe? Does main run? Are errors visible? Fixing even one item in the last five minutes, out loud, shows the self awareness interviewers look for.
- □ requirements written down?
- □ any class doing 3+ jobs?
- □ each pattern has a reason?
- □ shared state protected?
- □ new type = new class?
- □ names readable, money safe?
- □ main runs and prints?
- □ errors are visible?