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.

MethodReturns
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

Figure: Two drivers, one free spot: the same race as two bookings for the last seat

3. Parking lot design decisions to make first

  1. 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?
  2. 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?
  3. 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.
  4. 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, including isFull.
  5. What is the fee for a short stay? Work out ceil(hours) for a 5-minute stay and for an exactly 2-hour stay.
  6. 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 unparkVehicle on it returns -1, and getVehicleLocation returns null.

⚠️ Trap: initialize must clear everything from a previous lot, including history and rates.

⚠️ Trap: a truck takes 2 spots, so it lowers getTotalAvailableSpots and occupiedSpots by 2.

Before you submit

Further reading

After you've tried it

Submit first. The full walkthrough of one solution: Building a Multi-Floor Parking Lot System.

Open the problem