Designing a Parking Lot System
Apply your LLD knowledge to design a scalable and robust Parking Lot system, a classic interview problem.
Learning Goals
- Translate vague requirements into concrete classes and actors.
- Design a multi-level parking system using composition.
- Implement flexible pricing using the Strategy pattern.
Case Study 1: The Parking Lot
Designing a Parking Lot is a quintessential LLD challenge. It requires you to balance complex relationships, manage state, and ensure the system is extensible for future requirements (like electric charging spots or different vehicle types).
Step 1: Requirements Gathering
Before drawing a single line, we must define the scope:
- Vehicle Types: Car, Truck, Motorcycle, Van.
- Spot Types: Small, Medium, Large, Electric.
- Capacity: Multiple levels, each with many spots.
- Entry/Exit: Multiple gates. Entry issues a ticket; Exit calculates fees and processes payment.
- Pricing: Different rates for different vehicle types and durations.
Step 2: Core Actors and Classes
- Vehicle: Abstract base class for
Car,Truck, etc. - ParkingSpot: Holds a reference to a
Vehicle(if occupied). - Level: A collection of
ParkingSpots. - ParkingLot: A Singleton that manages multiple
Levels. - Ticket: Stores entry time, spot info, and vehicle info.
- Gate: Handles entry and exit logic.
- PricingStrategy: Interface for different billing rules.
Visualizing the static structure
Workflow: Parking a Vehicle
- 1Step 1
A vehicle arrives at an entry gate. The
Gateidentifies the vehicle type (e.g.,Motorcycle). - 2Step 2
The
ParkingLotiterates through itsLevelsand asks each level tofindAvailableSpot(vehicleType). This is a recursive check for availability. - 3Step 3
If a spot is found, it is marked as 'Occupied'. A
Ticketobject is created with a unique ID, the current timestamp, and the spot location. - 4Step 4
The
ParkingLotupdates its global capacity counter. If no spots are available, the gate display shows 'FULL'.
Knowledge Check
Why is the Strategy pattern used for pricing in the Parking Lot design?