Parking Lot
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-27
Problem Statement
Design a low-level architecture for a parking lot system that manages vehicle parking, slot allocation, fee calculation, and supports multiple vehicle types. The system should handle concurrent arrivals and departures efficiently.
Functional Requirements (FR)
Vehicle Entry & Exit
•Vehicles can enter and exit the parking lot.
•Capture entry time, exit time, and vehicle details.
Slot Management
•Manage parking slots for different vehicle types: Car, Bike, Truck, etc.
•Allocate nearest available slot upon arrival.
•Free the slot upon exit.
Parking Fee Calculation
•Calculate fees based on vehicle type and parking duration.
•Support hourly or flat-rate pricing.
Vehicle Types & Restrictions
•Different vehicle sizes may require different slot sizes.
•Optionally support reserved or VIP slots.
Reporting & Monitoring
•Track available and occupied slots.
•Provide reports on occupancy, revenue, and vehicle statistics.
Non-Functional Requirements (NFR)
•Concurrency Handling → Handle multiple vehicles entering/exiting simultaneously.
•Scalability → Support large parking lots with hundreds of slots.
•Availability → Ensure the system is available during peak hours.
•Low Latency → Fast slot allocation and fee calculation.
•Extensibility → Easily support new vehicle types or pricing models.
Core Classes & Entities
Vehicle
•vehicleId, vehicleType, licenseNumber
Slot
•slotId, slotType (Small, Medium, Large), isOccupied
ParkingLot
•lotId, name, location, slots
Ticket / ParkingTicket
•ticketId, vehicleId, slotId, entryTime, exitTime, amount
Payment
•paymentId, ticketId, amount, status, method
Key Methods / Operations
ParkingLot Service
•addSlot(slot) → add a new parking slot
•removeSlot(slotId) → remove a slot
•getAvailableSlots(vehicleType) → fetch free slots for a vehicle type
Vehicle Service
•vehicleEntry(vehicle) → allocate slot and generate ticket
•vehicleExit(ticketId) → calculate fee and free slot
Ticket Service
•generateTicket(vehicle, slot) → create parking ticket
•calculateFee(ticket) → compute parking charges based on duration and vehicle type
Payment Service
•processPayment(ticketId, amount, method)
Design Considerations
Concurrency & Slot Allocation
•Use synchronized methods or locks to prevent double allocation of the same slot.
Slot Prioritization
•Allocate nearest available slot or VIP slots first based on vehicle type and preferences.
Fee Calculation Flexibility
•Support multiple pricing strategies: flat, hourly, or dynamic based on demand.
Extensibility
•Add support for multiple parking lots, online booking, or automated gates.
Data Storage
•Store parking tickets, slot status, and payments in a relational or in-memory database.
Example Flow
1.Vehicle arrives → Vehicle Service checks available slots → Ticket Service generates ticket → Assign slot.
2.Vehicle parks → Slot status updated to occupied.
3.Vehicle leaves → Vehicle Service retrieves ticket → Calculate fee → Process payment → Slot freed.
4.ParkingLot Service updates available slot count → Report updated occupancy.