How to run an OOD question
It's not about UML perfection — it's about clean responsibilities and extensibility. Steps: clarify scope → identify entities (nouns) → assign responsibilities (verbs) → relationships → call out where behavior varies (Strategy) → walk a scenario.
Clarify first: multiple levels? vehicle sizes (motorcycle/car/bus)? pricing? payment? Pin scope: "single lot, multiple levels, three spot sizes, hourly pricing, ignore payments — OK?"
Entities & relationships
ParkingLotis the facade:parkVehicle/unpark. It owns levels and issues tickets.Levelowns spots and answersfindSpot(vehicle).ParkingSpothas a size andcanFit(vehicle); subclasses or asizeenum (prefer the enum +canFitover a deep spot hierarchy).Vehiclehierarchy (Car/Motorcycle/Bus) carries size.Ticketrecords spot + entry time for fee calculation.
Where the patterns live
- Strategy — spot assignment. "nearest", "first-available", "fit smallest
spot" are interchangeable
SpotAssignmentStrategyimplementations injected intoLevel. New policy = new class, no edits (Open/Closed). - Strategy — pricing.
FeeStrategy(flat, hourly, tiered) computed from the ticket. - Singleton (maybe). One
ParkingLotinstance — but inject it rather than reaching for a global.
Modeling every spot/vehicle combination as a subclass blows up fast. A size
enum plus spot.canFit(vehicle) keeps it small and handles "a car can use a
large spot if no compact is free" without new types.
Here's that spot-assignment strategy running. A motorcycle fits anywhere; a car needs a compact-or-larger spot; a large vehicle needs a large spot. Watch the scan reject spots that are taken or too small, then take the first that fits — swap the operations to test "two cars, one compact left".
dashed = free · a vehicle takes the nearest spot ranked its size or larger
1/287 spots: two motorcycle (M), three compact (C), two large (L). A vehicle parks in the nearest spot it fits — bigger spots accept smaller vehicles, never the reverse.
Think it through like the interview
Before reading further, try to run the method yourself — this widget walks the exact five moves, one at a time:
PROBLEMDesign the classes for a parking lot: vehicles arrive, get a spot that fits, receive a ticket, and pay on exit. You have ~35 minutes and a whiteboard.
- 1
Clarify scope
“What would change my design the most? Ask THOSE questions first.”
- 2
Nouns → entities
“Read the problem statement back. Which nouns survive as classes?”
unlocks after the stage above - 3
Verbs → responsibilities
“Who owns parkVehicle? Who owns findSpot? Why not all on ParkingLot?”
unlocks after the stage above - 4
Spot what varies → patterns
“Which decisions might change next month without the structure changing?”
unlocks after the stage above - 5
Walk a scenario + break it
“Narrate 'a car arrives'. Then: two cars, one spot left. What breaks?”
unlocks after the stage above
Walk a scenario + concurrency
"A car arrives": ParkingLot.parkVehicle(car) → ask each Level for a spot via
its assignment strategy → mark spot occupied → issue Ticket. Concurrency
follow-up: two cars racing for the last spot is the same race as a balance
double-spend — guard spot acquisition atomically (compare-and-set on the spot's
state, or a lock per level) so a spot is never double-assigned.
Practice — level up
A parking lot is an allocation problem: hand out a limited resource, take it back, and enforce "one holder at a time". These drills are the same shape.
Climb in order — every rung assumes the one above it. Solve on LeetCode, then tick it here; progress is saved on this device.
Warm-up — count what's free
Allocation with nothing but counters.Per-size counters — the simplest spot allocation.
Core — allocate & release specific slots
Now identity matters: which spot, given back.- Seat Reservation ManagerMedium
Reserve / unreserve numbered slots — parking spots by another name.
- Design Circular QueueMedium
Fixed capacity with free/occupied bookkeeping and wrap-around.
Stretch — paired entry/exit + fees
Ticket on the way in, charge on the way out.Check-in/check-out pairs and aggregate on exit — the ticket-and-fee half of the lot.