Notes
What is scored
First, know the scorecard. Interviewers check that you pin down requirements and scope, that your classes are small and well named, that you put interfaces where change is likely and pick patterns for a reason. They want code that runs and reads well, a sensible answer on thread safety, and calm answers to extension questions. Throughout, they score how clearly you explain. Hidden genius that is never said out loud earns nothing.
- • clear requirements + scope
- • sensible classes, small
- and named well
- • interfaces where change is
- likely, patterns with reason
- • working, readable code
- • thread safety handled
- • extension questions answered
- • you explain as you go
The time plan
Here is a time plan for sixty minutes. Five minutes to clarify requirements. Five for entities. Five for the class diagram and patterns. Five to walk the main flows. About twenty eight minutes to write code with a small demo. Five on concurrency, and the last seven on extensions and your questions. In a ninety minute machine coding round, the extra time mostly goes to code and testing. Check the clock at each step.
- 0-5 clarify requirements
- 5-10 entities
- 10-15 class diagram, patterns
- 15-20 core flows
- 20-48 code + a demo main
- 48-53 concurrency
- 53-60 extensions, questions
- 90 min: stretch code to
- about 50 min, test more
1 · Clarify
Step one, clarify. Our problem: a parcel locker. There are small, medium and large lockers. A courier deposits a parcel, the system chooses a locker and texts the customer a pickup code. The customer opens the locker with the code. Parcels not collected in three days expire. Several couriers can work at the same time. Say what is out of scope, like payments and multiple sites, and get a nod before moving on.
Ask questions whose answers change the code. Can a small parcel go into a large locker? Yes, but only if no small one is free. One parcel per locker? Yes. What happens on expiry? Free the locker and record it for return. Are codes single use? Yes. Each answer becomes a rule that lives in one specific class. Skip questions that change nothing, like the colour of the lockers.
- lockers: small, medium, large
- courier deposits a parcel
- system picks a locker and
- texts the customer a code
- customer picks up with code
- parcels expire after 3 days
- many couriers at once
- out of scope: payments,
- multiple sites, the app UI
- can a small parcel use a
- large locker? yes, if no
- small one is free
- one parcel per locker? yes
- wrong code: retry limit?
- out of scope for now
- expired: just free the
- locker and log it
- codes: unique, single use
2 · Entities
Step two, entities. Underline the nouns: locker, parcel, size, code, courier, customer. Keep only the ones that hold data or rules. Size becomes an enum with a fits method. Parcel and Assignment become records, because they never change. Locker has changing state, so it is a small class. The courier and customer only call the system, so they stay outside the model. LockerService is the single entry point.
- Size: enum, with fits()
- Parcel: record (id, size,
- phone)
- Locker: id, size, content
- Assignment: record (code,
- locker, parcel, time)
- LockerService: the facade
- courier, customer: callers,
- not classes
3 · Class diagram
Step three, a quick class diagram. LockerService has many lockers and tracks assignments by pickup code. An assignment points to one locker and one parcel, with the time it was stored. A locker contains zero or one parcel. Draw boxes, arrows and multiplicity, and say each relation as you draw it. This takes two minutes, and it becomes the map you follow while coding.
4 · Interfaces, patterns
Step four, interfaces and patterns. Put an interface wherever a what if question is likely. How lockers are chosen may change, so SlotPicker is a Strategy, starting with smallest fit. How customers are told may change from SMS to app push, so Notifier is an interface. Time is injected, so expiry can be tested without waiting three days. Name each pattern only with the pain it removes.
interface SlotPicker { // Strategy
List<Locker> candidates(
List<Locker> all, Size size);
}
interface Notifier { // SMS, app push
void send(String phone, String msg);
}
Supplier<Instant> now; // testable time 5 · Core flows
Step five, walk the core flows before coding. Deposit: the courier calls deposit with a parcel. The service asks the slot picker for candidate lockers in order of preference. It tries to claim the first one; if another courier just took it, it tries the next. It creates an assignment with a fresh code, stores it, and the notifier texts the code to the customer.
Pickup: the customer enters a code. The service removes the assignment for that code in one atomic step. If it was there, the locker is released and the door opens. A second attempt with the same code finds nothing and is refused, which gives single use codes for free. Walking flows like this catches missing methods before you write them, and shows the interviewer your plan.
6 · Code
Step six, code. Write in the order of your diagram: enum and records first, then Locker, then the interfaces, then the service, and last a main method that runs the flows. Here is deposit: loop over the candidates, claim a locker, create the assignment, notify, return the code. If nothing is free, return an empty optional instead of null. Keep methods short and talk while you type.
Optional<String> deposit(Parcel p) {
for (Locker l : picker.candidates(
lockers, p.size())) {
if (l.tryStore(p)) {
String code = codes.get();
byCode.put(code, new Assignment(
code, l, p, now.get()));
notifier.send(p.phone(), code);
return Optional.of(code); }
}
return Optional.empty(); } 7 · Concurrency
Step seven, concurrency. Name the shared state and the races. Two couriers may grab the same free locker: each locker holds its parcel in an atomic reference and claims it with compare and set, so exactly one wins and the other tries the next locker. Two pickups with one code: removing from a concurrent map returns the assignment to only one caller. Codes come from an atomic counter or a random id.
- two couriers, one free locker
- -> CAS on locker content;
- loser tries next one
- two pickups, one code
- -> ConcurrentHashMap.remove
- returns it to one caller
- code generator
- -> AtomicInteger or UUID
8 · Extensions
Step eight, extensions. The interviewer will push. Extra large lockers? Add an enum value. A fee for overdue days? A new pricing policy interface used at pickup. Many locker sites? A site class with its own service. App push instead of SMS? A new notifier. A different way to choose lockers? A new slot picker. If your seams were chosen well, each answer is a small, local change, and you can say exactly where it goes.
- extra-large lockers?
- add an enum value
- fee for overdue days?
- a PricingPolicy interface
- many locker sites?
- LockerSite, service per site
- app push instead of SMS?
- a new Notifier
- nearest site first?
- a new SlotPicker
Show it runs
Finish coding with a demo that proves it works. Deposit a few parcels. Pick one up. Try the same code again and a wrong code. Fill every locker and show the clear answer. Move the injected clock forward and show expiry. Finally, run several couriers on threads at once and show no locker was used twice. Five minutes of demo builds more trust than five more classes.
- • deposit three parcels
- • pick one up with its code
- • same code again: refused
- • wrong code: refused
- • lockers full: clear answer
- • move the clock: expiry
- • many couriers at once:
- no locker used twice
How to behave
A few habits matter as much as the design. Think out loud, so the interviewer can follow and help. State trade-offs, why this and not that. When the interviewer drops a hint, take it; it is usually a gift. Get a simple working version first, then improve it. And if you are stuck, say what you are trying and simplify the problem. Silence is the hardest thing to score.
- • think out loud
- • state trade-offs: why X
- and not Y
- • take hints, do not argue
- • working simple version
- first, then improve
- • short methods, clear names
- • if stuck: say what you
- are trying, simplify