Bird
0
0
LLDsystem_design~25 mins

Why parking lot is a classic LLD problem - Design It to Understand It

Choose your learning style9 modes available
Design: Parking Lot System
Focus on core parking lot operations and spot management. Exclude payment gateway integration and mobile app UI.
Functional Requirements
FR1: Allow vehicles to enter and exit the parking lot
FR2: Track available parking spots by type (e.g., motorcycle, car, truck)
FR3: Assign parking spots to vehicles based on availability and vehicle type
FR4: Calculate parking fees based on duration
FR5: Support multiple floors or sections
FR6: Handle different vehicle sizes and spot sizes
Non-Functional Requirements
NFR1: Support up to 500 vehicles simultaneously
NFR2: Response time for spot assignment under 200ms
NFR3: System should be easy to extend for new vehicle or spot types
NFR4: Maintain consistency of spot availability in concurrent scenarios
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Vehicle and Parking Spot classes with attributes
Parking Lot manager to assign and release spots
Data store to keep track of spot availability
Fee calculator based on parking duration
User interface or API for entry and exit
Design Patterns
Factory pattern for creating vehicle and spot objects
Singleton pattern for Parking Lot manager to ensure single source of truth
Observer pattern to notify when spots become free
Strategy pattern for different fee calculation methods
Reference Architecture
  +-------------------+
  |   Entry Gate      |
  +---------+---------+
            |
            v
  +-------------------+       +-------------------+
  | Parking Lot Manager|<----->| Spot Availability  |
  +---------+---------+       +---------+---------+
            |                           |
            v                           v
  +-------------------+       +-------------------+
  | Vehicle Registry   |       | Fee Calculator    |
  +-------------------+       +-------------------+
            |
            v
  +-------------------+
  | Exit Gate         |
  +-------------------+
Components
Vehicle
Class/Interface
Represents different vehicle types with size and attributes
ParkingSpot
Class/Interface
Represents parking spots with size and availability status
ParkingLotManager
Singleton Class
Manages spot assignment, release, and overall parking lot state
SpotAvailabilityStore
In-memory data structure or database
Tracks which spots are free or occupied
FeeCalculator
Class with strategy pattern
Calculates parking fees based on vehicle type and duration
EntryGate and ExitGate
API endpoints or UI components
Handle vehicle entry and exit events
Request Flow
1. Vehicle arrives at Entry Gate and requests parking
2. ParkingLotManager checks SpotAvailabilityStore for suitable spot
3. Assigns spot and marks it occupied
4. Vehicle parks and entry time is recorded in Vehicle Registry
5. Upon exit, vehicle reports at Exit Gate
6. ParkingLotManager calculates fee using FeeCalculator based on entry and exit times
7. Spot is marked free in SpotAvailabilityStore
Database Schema
Entities: Vehicle (id, type, license_plate), ParkingSpot (id, type, floor, is_occupied), ParkingSession (id, vehicle_id, spot_id, entry_time, exit_time, fee), Relationships: Vehicle 1:N ParkingSession, ParkingSpot 1:N ParkingSession
Scaling Discussion
Bottlenecks
Single ParkingLotManager becomes a bottleneck with many concurrent requests
SpotAvailabilityStore may have contention with many updates
FeeCalculator may slow down if complex fee rules increase
Database can become slow with many ParkingSession records
Solutions
Partition parking lot into zones with separate managers to distribute load
Use distributed cache like Redis for SpotAvailabilityStore with atomic operations
Optimize FeeCalculator with caching or precomputed rates
Archive old ParkingSession records and index database for faster queries
Interview Tips
Time: Spend 10 minutes clarifying requirements and constraints, 20 minutes designing classes and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing
Explain how vehicle and spot types affect design
Discuss concurrency and consistency in spot assignment
Highlight extensibility for new vehicle or spot types
Mention how fee calculation can vary and be modular
Show awareness of bottlenecks and scaling strategies