LLD Guide · Module 2: Problem prep · Lesson 02.05
Prep: Parking Lot System
Spots, tickets and fees in Postgres, trucks that need two spots in a row, and no double-booking under load.
By Cruscible Engineering · Published
A parking lot is a booking system in disguise: the last free spot is the seat everyone wants at once.
This lesson prepares you for Build a Parking Lot System. It doesn't solve it.
1. What the parking lot contract asks for
A multi-floor lot for cars, motorcycles and trucks, with all state in Postgres. A car or motorcycle takes one spot; a truck takes two consecutive spots on the same floor. Parking issues a ticket; unparking frees the spots and charges ceil(hours) × hourlyRate.
| Method | Returns |
|---|---|
initialize(floors, spotsPerFloor) | nothing; clears all data and builds the lot |
parkVehicle(number, type) | a unique ticket id on the lowest floor with room, or null if there's no room for that type |
unparkVehicle(ticketId) | the fee (≥ 0), or -1 for an unknown or already used ticket |
getVehicleLocation(ticketId) | {floor, spotNumber, vehicleType} (1-based), or null once unparked |
getAvailableSpots(floor, type) | free spots on that floor; for TRUCK, free pairs of consecutive spots |
getTotalAvailableSpots() | free individual spots in the whole lot |
isFull(type) | true when no spot (or pair, for trucks) fits that type |
getFeeRate(type) / setFeeRate(type, rate) | the hourly rate per type |
getVehicleHistory(number) | every park and unpark for that vehicle, with fees |
getLotStats() | totalSpots, occupiedSpots, occupancyPercent, and a byType breakdown |
💡 Tip: spots are shared by every vehicle type:
initialize(3, 10)means 30 spots in total, not 30 per type.
2. Lessons to read before the parking lot
- 01.04 Postgres for LLD: conditional updates,
FOR UPDATE SKIP LOCKED, and constraints. This problem is built on them. - 01.05 State machines: a ticket is active, then used, and never active again.
Figure: Two drivers, one free spot: the same race as two bookings for the last seat
- Read, then write: both drivers' transactions see the spot free and both take it.
- Conditional UPDATE: the first claims the spot (1 row); the second changes 0 rows and looks for another spot.
3. Parking lot design decisions to make first
- What are your tables? Spots, tickets and fee rates at least. Which columns does each need, and which constraint stops two vehicles holding one spot?
- How do you claim a spot atomically? One conditional statement, or a transaction that locks the row it picks? What does a second, concurrent caller see?
- How do you claim two consecutive spots for a truck? Both must be free, adjacent, on the same floor, and taken together or not at all.
- What does
getAvailableSpots(floor, "TRUCK")count? The brief says pairs of consecutive free spots but not whether pairs may overlap. Pick a definition (for example, how many more trucks could park on that floor) and use it everywhere, includingisFull. - What is the fee for a short stay? Work out
ceil(hours)for a 5-minute stay and for an exactly 2-hour stay. - What stops a vehicle from parking twice? The contract says a parked vehicle can't park again until it leaves.
4. Parking lot pitfalls
⚠️ Trap: all state must be in the database. Spots in a Java array plus tickets in Postgres won't pass "no in-process data structures".
⚠️ Trap: a used ticket is dead. A second
unparkVehicleon it returns-1, andgetVehicleLocationreturnsnull.
⚠️ Trap:
initializemust clear everything from a previous lot, including history and rates.
⚠️ Trap: a truck takes 2 spots, so it lowers
getTotalAvailableSpotsandoccupiedSpotsby 2.
Before you submit
- All spots, tickets, rates and history are in Postgres
initialize(floors, spotsPerFloor)givesfloors × spotsPerFloorfree spots and clears old data- Vehicles go to the lowest floor with room
- A truck gets two consecutive spots on one floor, or doesn't park
parkVehiclereturnsnullwhen nothing fits, and a vehicle already parked can't park again- Ticket ids are unique for the lifetime of the lot
unparkVehiclechargesceil(hours) × rateand returns-1for an unknown or used ticket- A used ticket's location is
null, and its spots are free again getAvailableSpots,getTotalAvailableSpots,isFullandgetLotStatsagree with each other- History lists every stay for the vehicle number, with its fee
- Concurrent parks never put two vehicles in one spot
Further reading
SELECT … FOR UPDATEandSKIP LOCKED: the locking clause (PostgreSQL docs)- Constraints (PostgreSQL docs)
- Explicit locking (PostgreSQL docs)
After you've tried it
Submit first. The full walkthrough of one solution: Building a Multi-Floor Parking Lot System.