Parking Lot
Vehicle Parking System
Design a multi-floor parking lot that assigns each vehicle a spot it fits, issues a ticket, and charges for the stay, while many gates park and release cars at once.
Overview
A parking lot has floors, each floor has spots of a few sizes, and vehicles of a few types arrive at the gates. The system must find a spot the vehicle fits, hand back a ticket, and when the vehicle leaves, free the spot and compute the fee. Everyone knows the domain, so the interviewer can spend the whole hour on your design choices.
The problem tests three things. First, how you model a rule table (which vehicle fits which spot) so that a new vehicle type is a data change, not a new branch in the code. Second, how you keep allocation fast and safe when several gates park vehicles at the same moment: two gates must never be given the same spot. Third, whether you can separate the parts that change often (pricing) from the parts that do not (the layout), which is where the Strategy pattern earns its place.
A strong answer names the entities in the first two minutes, states the fit rule as a table, explains where the lock lives and why, and writes a small program that a test can drive with a fake clock.
Scoping the problem
Before writing code, ask these questions; each answer changes the design a little.
- Which vehicle types and spot sizes exist? The answer fixes the fit table. Usually: bike, car and truck; small, compact and large spots.
- May a smaller vehicle use a bigger spot? This decides whether the fit rule is a set of sizes or one size. Usually yes: a car may take a large spot.
- Prefer the smallest fit, or the nearest floor? Best fit keeps large spots for trucks; nearest floor is friendlier to drivers. Usually best fit, then the lowest floor.
- How is the fee computed? Hourly, flat daily or per type; this is the part that changes most. Usually per started hour, by vehicle type.
- How many entry and exit gates run at once? This decides whether locks are needed and how fine they are. Usually several, so allocation must be safe.
- What does a full lot do? It can refuse at the gate or queue the driver. Usually it refuses with a clear error.
- Do tickets or payments persist across restarts? That adds a store, which is not part of the core. Treat it as a follow-up.
Functional requirements
- Several floors, each with spots of three sizes: small, compact, large
- Three vehicle types: bike, car, truck; each may use a fixed set of spot sizes
- park(vehicle, now) assigns the smallest fitting free spot on the lowest floor and returns a ticket, or refuses when none fits
- leave(ticket_id, now) frees the spot and returns the fee; a ticket can leave once
- Fees come from a pluggable pricing strategy (per started hour by vehicle type, or flat per day)
- availability() reports free spots per floor and size
Non-functional requirements
- Taking or returning a spot is O(1): free lists per size, no scan of occupied spots
- Safe for many gates at once: a spot is never given to two vehicles
- Locks are per floor, so floors do not contend with each other
- Adding a vehicle type or a pricing plan changes data or adds a class; no method is edited
- Deterministic: time is injected, so fees are exact in tests
Interview questions
- How would you add electric vehicle charging spots?
- Two gates park a car at the same moment. Walk me through what could go wrong and how your code prevents it.
- Why best fit across all floors instead of the first free spot on the nearest floor?
- How would you make tickets survive a restart?
- What would you show on the display board at the entrance, and how do you keep it correct?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio