How to Design Parking Lot LLD: Simple and Scalable Approach
To design a parking lot LLD, create
classes for ParkingLot, ParkingSpot, Vehicle, and Ticket. Define clear methods for parking, leaving, and spot management, ensuring scalability by using abstraction and composition.Syntax
The main classes and their roles in parking lot LLD:
- ParkingLot: Manages parking spots and tickets.
- ParkingSpot: Represents a single spot with size and availability.
- Vehicle: Represents vehicles with type and size.
- Ticket: Issued when a vehicle parks, tracks spot and time.
Key methods include parkVehicle(), leaveSpot(), and findAvailableSpot().
javascript
class ParkingLot { constructor(spots) { this.spots = spots; // array of ParkingSpot this.tickets = new Map(); // vehicleId -> Ticket } parkVehicle(vehicle) { const spot = this.findAvailableSpot(vehicle); if (!spot) return null; spot.occupy(vehicle); const ticket = new Ticket(vehicle.id, spot.id, Date.now()); this.tickets.set(vehicle.id, ticket); return ticket; } leaveSpot(vehicleId) { const ticket = this.tickets.get(vehicleId); if (!ticket) return false; const spot = this.spots.find(s => s.id === ticket.spotId); spot.free(); this.tickets.delete(vehicleId); return true; } findAvailableSpot(vehicle) { return this.spots.find(spot => spot.isAvailable() && spot.canFit(vehicle)); } } class ParkingSpot { constructor(id, size) { this.id = id; this.size = size; // e.g., 'small', 'medium', 'large' this.vehicle = null; } isAvailable() { return this.vehicle === null; } canFit(vehicle) { const sizes = { small: 1, medium: 2, large: 3 }; return sizes[this.size] >= sizes[vehicle.size]; } occupy(vehicle) { this.vehicle = vehicle; } free() { this.vehicle = null; } } class Vehicle { constructor(id, size) { this.id = id; this.size = size; // 'small', 'medium', 'large' } } class Ticket { constructor(vehicleId, spotId, entryTime) { this.vehicleId = vehicleId; this.spotId = spotId; this.entryTime = entryTime; } }
Example
This example shows parking a vehicle and then leaving the spot, demonstrating spot allocation and ticket management.
javascript
const spots = [ new ParkingSpot(1, 'small'), new ParkingSpot(2, 'medium'), new ParkingSpot(3, 'large') ]; const parkingLot = new ParkingLot(spots); const vehicle1 = new Vehicle('V1', 'small'); const ticket1 = parkingLot.parkVehicle(vehicle1); console.log('Ticket issued:', ticket1); const left = parkingLot.leaveSpot(vehicle1.id); console.log('Vehicle left:', left); const ticket2 = parkingLot.parkVehicle(new Vehicle('V2', 'large')); console.log('Ticket issued for large vehicle:', ticket2);
Output
Ticket issued: Ticket { vehicleId: 'V1', spotId: 1, entryTime: 1680000000000 }
Vehicle left: true
Ticket issued for large vehicle: Ticket { vehicleId: 'V2', spotId: 3, entryTime: 1680000000100 }
Common Pitfalls
- Not handling different vehicle sizes properly can cause wrong spot allocation.
- Failing to free spots when vehicles leave leads to full parking even if spots are free.
- Not tracking tickets properly causes issues in billing or spot management.
- Mixing responsibilities in one class reduces maintainability.
javascript
/* Wrong: ParkingSpot class handles ticket creation (mixing concerns) */ class ParkingSpot { occupy(vehicle) { this.vehicle = vehicle; this.ticket = new Ticket(vehicle.id, this.id, Date.now()); // Wrong: Ticket should be managed by ParkingLot } } /* Right: ParkingLot manages tickets separately */ class ParkingLot { parkVehicle(vehicle) { const spot = this.findAvailableSpot(vehicle); if (!spot) return null; spot.occupy(vehicle); const ticket = new Ticket(vehicle.id, spot.id, Date.now()); this.tickets.set(vehicle.id, ticket); return ticket; } }
Quick Reference
- ParkingLot: Manages spots and tickets.
- ParkingSpot: Knows size and availability.
- Vehicle: Has size and ID.
- Ticket: Tracks parking info.
- Use clear methods:
parkVehicle(),leaveSpot(),findAvailableSpot(). - Keep responsibilities separated for easy maintenance.
Key Takeaways
Separate concerns by defining clear classes for ParkingLot, ParkingSpot, Vehicle, and Ticket.
Handle different vehicle sizes to allocate appropriate parking spots.
Always free spots when vehicles leave to keep availability accurate.
Manage tickets centrally in ParkingLot to track parked vehicles and spots.
Use simple, clear methods for parking and leaving operations to maintain scalability.