Design a Parking Lot System
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-08-24
Problem Statement
Design and implement a thread-safe, scalable parking lot system that manages vehicles, parking spots, and billing. Focus on object modeling, parking rules, concurrency, and efficient space management.
Functional Requirements (FR)
Parking Management
•Support multiple types of vehicles: Car, Bike, Truck, Electric Vehicle.
•Support multiple types of parking spots: Compact, Regular, Large, EV Charging.
•Assign vehicles to appropriate spots based on size/type.
•Track occupancy of spots in real-time.
Entry & Exit
•Allow vehicles to enter and exit.
•Calculate parking fees based on duration and vehicle type.
•Generate tickets for vehicles entering the parking lot.
Availability & Queries
•Check available spots by type.
•Find nearest available spot for a vehicle.
Billing & Payment
•Calculate parking charges.
•Support payment at exit or pre-paid options.
Advanced Features (Optional)
•Reserve spots in advance.
•Support multi-level parking with floors.
•Real-time notifications for available spots.
Non-Functional Requirements (NFR)
•Concurrency → Handle multiple vehicles entering/exiting simultaneously.
•Scalability → Support multiple floors and large parking lots.
•Availability → Spot status should be accurate in real-time.
•Extensibility → Support new vehicle types, spot types, or dynamic pricing.
Core Entities & Attributes
Vehicle
•vehicleId, type (Car/Bike/Truck/EV), licensePlate
ParkingSpot
•spotId, type (Compact/Regular/Large/EV), floor, occupied, vehicleId
Ticket
•ticketId, vehicleId, spotId, entryTime, exitTime, fee
ParkingLot / Floor
•lotId, name, floors[], spots[]
Core Components
ParkingLotManager
•Assigns vehicles to available spots.
•Tracks spot occupancy.
TicketService
•Issues tickets on entry.
•Updates exit time and calculates fees.
PaymentService
•Handles billing and payment processing.
SpotAvailabilityService
•Provides queries for available spots by type and floor.
NotificationService (Optional)
•Alerts users about available spots or low availability.
APIs / Operations
Vehicle APIs
•enterParking(vehicle) → Assign spot, issue ticket.
•exitParking(ticketId) → Free spot, calculate fee, close ticket.
•getVehicleTicket(vehicleId)
Spot APIs
•getAvailableSpots(type) → Return nearest available spot.
•reserveSpot(vehicleId, type) (Optional)
Payment APIs
•calculateFee(ticketId)
•pay(ticketId, amount, method)
LLD Design Goals
•Class Diagram → ParkingLot, Floor, ParkingSpot, Vehicle, Ticket, ParkingLotManager, PaymentService.
Design Patterns:
•Singleton → Single instance of ParkingLotManager.
•Factory → For creating different types of Vehicle and ParkingSpot.
•Observer → Notify when spots become available.
•Concurrency Handling →Thread-safe allocation of parking spots. Locks or synchronized structures to prevent double allocation.
•Trade-offs → Pre-allocate spots vs. allocate on-demand; real-time availability vs. performance.