The LLD Interview Method
A step-by-step process for LLD and machine-coding rounds (requirements → entities → relationships → APIs → patterns → code → concurrency → extensions), how to manage time, thread-safety techniques for in-memory designs, testing your design live, and the mistakes that sink candidates.
The 8-step method
1. Requirements
Ask about:
- Actors (customer, admin, driver) and use cases for each.
- Scope: in-memory only? persistence? UI/CLI? payments mocked?
- Rules: pricing, limits, cancellation policy, priorities.
- Concurrency: multiple entry points/users at once?
- Extensibility expectations ("we'll add EV charging spots later").
Write a short numbered list and confirm it.
2. Entities and responsibilities
Underline nouns in the requirements → candidate classes; verbs → methods. Decide:
- Entities (identity):
ParkingLot,Spot,Ticket. - Value objects:
Money,TimeRange,Location. - Enums:
VehicleType,SpotType,TicketStatus. - Services for workflows that span entities:
ParkingService,PaymentService. - Repositories (interfaces) for storage, in-memory implementations.
3. Relationships
Composition vs aggregation vs association, multiplicities, inheritance only for genuine is-a.
4. APIs and flows
public interface ParkingService {
Ticket park(Vehicle vehicle, EntryGateId gate); // throws NoSpotAvailable
Receipt exit(TicketId ticket, PaymentMethod method); // throws InvalidTicket
Map<SpotType, Integer> availability(FloorId floor);
}
Walk through each flow aloud ("a car arrives at gate 2…") — it often reveals missing pieces.
5. Patterns where they earn their keep
Point to the axis of change: "pricing differs by vehicle type and time → Strategy"; "spot assignment policy may change (nearest to entrance vs fill floors bottom-up) → Strategy"; "order lifecycle → State"; "display boards update on changes → Observer".
6. Code
- Enums, interfaces and models first, then services, then a
main/test driving the flows. - Validate inputs; throw domain-specific exceptions.
- Keep I/O (printing, parsing commands) out of the domain.
7. Concurrency and edge cases
| Shared resource | Race | Fix |
|---|---|---|
| parking spots | two gates assign the same spot | lock per spot / atomic tryOccupy() / concurrent free-spot queues |
| seats in a show | double booking | per-show lock, seat hold with expiry, CAS on seat status |
| elevator requests | lost/duplicate requests | thread-safe queues, single scheduler thread |
| account balances | double spend | per-account lock, atomic update |
| counters/IDs | duplicate IDs | AtomicLong, UUIDs |
// lock-free spot claim: only one thread can flip FREE → OCCUPIED
final class Spot {
private final AtomicReference<Vehicle> occupant = new AtomicReference<>();
boolean tryOccupy(Vehicle v) { return occupant.compareAndSet(null, v); }
void release() { occupant.set(null); }
}
8. Extensions and testing
Show how a new requirement lands: a new VehicleType → enum value + pricing strategy entry; a new split type → new SplitStrategy class. Mention unit tests for strategies/state transitions and a concurrency test (many threads parking simultaneously, assert no spot is double-assigned).
Package structure for machine-coding rounds
src/
model/ Vehicle, Spot, Ticket, enums, value objects
service/ ParkingService, PricingStrategy implementations
repository/ interfaces + InMemory implementations
exception/ domain exceptions
Main.java wires everything, runs sample flows / reads commands
test/
ParkingServiceTest.java
Common mistakes
| Mistake | Better |
|---|---|
| God class doing everything | split responsibilities (SRP), services per use case |
Inheritance for every variation (RedCar, BlueCar) | fields/enums; strategies for behaviour |
| Strings for types/states | enums |
| Patterns without a reason | name the change each pattern absorbs |
| Ignoring concurrency | identify shared state; lock per entity |
| Printing inside domain logic | return values; I/O at the edges |
| No runnable flow at the end | build vertical slices early |
| Public mutable fields | encapsulation, immutable value objects |
| Not validating input / states | fail fast with clear exceptions |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.