Design a parking lot
Design the classes for a multi-floor car park that assigns spots, issues tickets and charges fees.
Why interviewers ask it
It's the classic first LLD question: the entities are clear, there are natural places for patterns (pricing, spot assignment), and there's a real concurrency problem.
Functional requirements
- Several floors, with spots of different sizes (small, compact, large).
- Vehicles (bike, car, truck) park in a suitable free spot.
- Issue a ticket on entry; calculate the fee on exit; take payment.
- Show free spots per floor on display boards.
Non-functional requirements
- Two vehicles must never get the same spot, even at different gates.
- Easy to add new vehicle types or pricing rules.
From version 1 to the final design
- Version 1: one floor, one gate, one spot size, a flat fee. Classes: ParkingLot, Spot, Vehicle, Ticket.
- Version 2: several spot sizes and vehicle types, a rule for which vehicle fits which spot, and pricing as a strategy.
- Version 3: several floors and gates, with display boards observing the free counts.
- Version 4: concurrency — claiming a spot atomically — and storing state in a database so gates on different servers agree.
The design, step by step
- Entry: a vehicle arrives → the assignment strategy finds a free spot that fits → the spot is marked taken in one atomic step → a ticket is issued.
- Exit: scan the ticket → the pricing strategy works out the fee from the time parked → payment → the spot is freed.
- Display boards subscribe to spot changes (Observer) and update their counts.
Deep dives
- Concurrency: two gates assigning the same spot. Use a lock (per floor, or for the whole lot), or an atomic compare-and-set on the spot.
- Fit rules: can a bike use a car spot when bike spots are full? Decide, and say it out loud.
- Lost ticket: charge the maximum daily fee.
- Extensibility: electric-car charging spots become a new spot type with their own pricing strategy.
Common mistakes
- Putting pricing logic inside Ticket or Vehicle.
- Ignoring two gates acting at the same moment.
- Vehicle subclasses that override lots of behaviour instead of just declaring their size.
Not true: Pricing rules: kept inside the Ticket class.
In fact: Prices change often, and for many reasons. Keep them in a separate pricing strategy, so that a new tariff does not touch Ticket or Vehicle.
In the interview
Core classes: ParkingLot, Floor, Spot, the Vehicle types, Ticket and Payment. Spot assignment and pricing are strategies, so the rules can change without touching the rest. Parking is one atomic step — a lock or a compare-and-set — so two gates can't take the same spot. Display boards observe spot changes.
Part of Unit 7: Low-level design questions in the free System Design course.