Parking Lot · 9002
A medium low-level design interview question on inheritance, polymorphism, strategy, encapsulation. 8 scenario tests.
SYNOPSIS
interviewer: Design a parking lot.
A multi-level garage. Drivers take a ticket at the entrance and pay at the exit. You have about 45 minutes.
QUESTIONS TO ASK
Interviews start vague on purpose. Strong candidates ask before they draw; in the studio the interviewer answers these and the answers become your requirements.
- What kinds of vehicles and spots are there?
- How many levels, and are they all the same?
- Which spot should a vehicle get?
- What does the driver get at the entrance?
- How is the fee calculated?
- What if the same car parks twice, or a ticket is used twice?
- Do we handle card payments and receipts?
- Is there a board showing free spots?
- Several entrances at the same time?
DESCRIPTION
The full statement, as the studio shows it at the implement step.
Design the software for a multi-level parking lot that hands out tickets at the entrance and charges at the exit.
requirements
- Three spot sizes: S, M, L. Each level has a number of spots of each size, e.g. levels: [{ S: 2, M: 3, L: 1 }, { S: 0, M: 4, L: 2 }].
- Three vehicle types: Bike (size S), Car (M) and Truck (L). A vehicle fits a spot of its own size or bigger: a bike can use S, M or L; a car M or L; a truck only L.
- park(vehicle) returns a ticket, or null when nothing fits.
- leave(ticketId) frees the spot and returns { plate, fee }.
- available(size) is the number of free spots of exactly that size.
- isFull() is true when not even a bike could park.
where a vehicle goes
- The lowest level that has a fitting free spot.
- On that level, the smallest fitting size.
- Within that size, the lowest spot index (spots are numbered 0, 1, 2… per size per level).
tickets and fees
- Ticket: { id, plate, level, spot: { size, index }, entry }. Ids are "T1", "T2", … in the order tickets are issued.
- Hourly rate by the vehicle's size (not the spot's): options.rates, default { S: 10, M: 20, L: 40 }.
- Fee = rate × hours parked, rounded up to whole hours, minimum 1 hour.
- Time comes from options.clock (milliseconds, default Date.now).
rules
- Parking a plate that is already inside throws "already parked".
- leave() with an unknown or already used ticket throws "unknown ticket".
think about
- Adding an electric spot with chargers, or a monthly-pass discount, without rewriting ParkingLot.
GRADING
The studio runs the interview in seven steps (clarify, entities, class diagram, patterns, sequence, implement, review) and a coach grades each one live: the core objects you found, the relations between them (ownership vs. use, the right end of the diamond), responsibilities, whether your patterns really have the shape of the pattern, and a sequence diagram of the main use case. Then you implement it in JavaScript and run its 8 scenario tests in your browser. The review gives a score out of 100, a hire signal and a reference design to compare.
FOLLOW-UPS
What the interviewer asks when you finish early:
- Several entrances issue tickets at the same time: what has to be atomic, and where would you put the lock?
- A display board per level shows free spots live. Which pattern keeps the boards in sync without ParkingLot knowing about them?