Parking Lot System design
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-30
Problem Statement
Design a Parking Lot Management System that manages entry/exit of vehicles, allocation of parking spots, and calculation of parking fees. The system should be extensible for different types of vehicles and pricing models.
Functional Requirements
1.Entities
•Vehicles: Car, Bike, Truck (can add more in future).
•Parking Spots: Different sizes (Small, Medium, Large).
•Parking Lot: A collection of parking levels/floors.
2.Core Features
•Assign nearest available parking spot when a vehicle enters.
•Free the spot when the vehicle exits.
•Calculate parking fee based on time parked and vehicle type.
•Issue tickets at entry and validate them at exit.
3.APIs / Operations
•parkVehicle(Vehicle v) → returns a Ticket if parking available, otherwise null.
•unparkVehicle(Ticket t) → frees the spot, calculates fees, and closes the ticket.
•getAvailableSpots() → returns count of free spots per type.
•addParkingSpot(Level, SpotType) → add new spots dynamically.
4.Constraints / Edge Cases
•What happens when the lot is full? → return "No space available".
•What if a large vehicle comes but only small spots are left? → reject parking.
•Ensure unique mapping of Ticket ↔ Spot.
•Handle multiple levels/floors efficiently.
Non-Functional Requirements
•Scalability: System should support multiple levels and thousands of spots.
•Extensibility: Easy to add new vehicle types or pricing strategies.
•Thread-Safety: Concurrent parking/unparking requests should not corrupt data.
•Performance: Parking spot allocation should be O(1) or O(log n), not linear scan.
Design Hints (Interview Direction)
•Classes:
•ParkingLot, Level, ParkingSpot, Vehicle, Ticket, Payment.
•Inheritance:
•Vehicle → Car, Bike, Truck.
•ParkingSpot → SmallSpot, MediumSpot, LargeSpot.
•Patterns:
•Strategy Pattern for pricing.
•Singleton for ParkingLot (only one per system).
•Factory for vehicle/spot creation.
•Data Structures:
•Use Map<VehicleType, PriorityQueue<Spot>> for fast allocation of nearest available spot.
Example Flow
1.Car enters → parkVehicle(car) → assigns nearest MediumSpot → issues Ticket.
2.Car exits → unparkVehicle(ticket) → frees spot → calculates fee = (exitTime - entryTime) × rate.
3.Another vehicle can now occupy that spot.
👉 This problem checks OOP design, clean abstractions, extensibility, handling concurrency, and tradeoffs.