2dbi

Design a Parking Garage System

viaLeetCode

Problem Object-oriented design of a parking garage: multiple levels, spots of different sizes, vehicle entry/exit with ticketing and pricing.

Requirements

  • park(vehicle) → assigns a suitable free spot and issues a Ticket; unpark(ticket) → frees the spot and computes the fee.
  • Spot compatibility: motorcycle fits any spot; car needs compact+; bus needs N consecutive large spots (the classic twist — clarify if in scope).
  • Real-time availability per level/size; pricing by vehicle type and duration.

Core design

  • Classes: Vehicle (abstract; Motorcycle/Car/Bus with size), ParkingSpot(size, level, state), Level(spots, free-count per size), ParkingGarage (levels, spot-assignment), Ticket(vehicle, spot, entryTime), PricingStrategy interface (hourly, flat, tiered — strategy pattern), Payment.
  • Spot assignment service keeps free lists (per level per size) — e.g. TreeSet/queue per size for O(log n) allocate/free — rather than scanning all spots.

Discussion points

  • Where assignment logic lives (garage vs level) and supporting policies like "nearest to entrance" (min-heap by distance).
  • Concurrency: two cars racing for the last spot — lock per level / atomic free-count, and idempotent unpark.
  • Extensibility: EV spots with chargers, reservations, dynamic pricing — which abstractions absorb each change.
  • hourly fee edge cases: grace period, lost ticket, overnight caps.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account