Design Parking Lot
Design a multi-level parking lot system
Overview
Design a parking lot system that can handle multiple floors, different vehicle types, and parking spot allocation.
Requirements:
- Multiple floors with multiple parking spots
- Different spot types: Compact, Large, Handicapped, Motorcycle
- Different vehicle types: Car, Truck, Motorcycle, Van
- Find available spots based on vehicle type
- Park and unpark vehicles
- Calculate parking fees based on duration
Key Concepts
Object-oriented design and class relationships
Enum for vehicle and spot types
Composition (ParkingLot has Floors, Floor has Spots)
Strategy pattern for pricing
Singleton for ParkingLot
Code Example
1// Enums
2enum VehicleType {
3 CAR, TRUCK, MOTORCYCLE, VAN
4}
5
6enum ParkingSpotType {
7 COMPACT, LARGE, HANDICAPPED, MOTORCYCLE
8}
9
10// Vehicle class
11abstract class Vehicle {
12 protected String licensePlate;
13 protected VehicleType type;
14
15 public Vehicle(String licensePlate, VehicleType type) {
16 this.licensePlate = licensePlate;
17 this.type = type;
18 }
19
20 public VehicleType getType() {
21 return type;
22 }
23}
24
25class Car extends Vehicle {
26 public Car(String licensePlate) {
27 super(licensePlate, VehicleType.CAR);
28 }
29}
30
31class Motorcycle extends Vehicle {
32 public Motorcycle(String licensePlate) {
33 super(licensePlate, VehicleType.MOTORCYCLE);
34 }
35}
36
37// Parking Spot
38class ParkingSpot {
39 private int id;
40 private ParkingSpotType type;
41 private Vehicle vehicle;
42 private boolean isOccupied;
43
44 public ParkingSpot(int id, ParkingSpotType type) {
45 this.id = id;
46 this.type = type;
47 this.isOccupied = false;
48 }
49
50 public boolean canFitVehicle(Vehicle vehicle) {
51 if (isOccupied) return false;
52
53 switch (vehicle.getType()) {
54 case MOTORCYCLE:
55 return true; // Can fit in any spot
56 case CAR:
57 return type == ParkingSpotType.COMPACT ||
58 type == ParkingSpotType.LARGE;
59 case TRUCK:
60 return type == ParkingSpotType.LARGE;
61 default:
62 return false;
63 }
64 }
65
66 public void parkVehicle(Vehicle vehicle) {
67 this.vehicle = vehicle;
68 this.isOccupied = true;
69 }
70
71 public void removeVehicle() {
72 this.vehicle = null;
73 this.isOccupied = false;
74 }
75}
76
77// Parking Floor
78class ParkingFloor {
79 private int floorNumber;
80 private List<ParkingSpot> spots;
81
82 public ParkingFloor(int floorNumber) {
83 this.floorNumber = floorNumber;
84 this.spots = new ArrayList<>();
85 }
86
87 public void addSpot(ParkingSpot spot) {
88 spots.add(spot);
89 }
90
91 public ParkingSpot findAvailableSpot(Vehicle vehicle) {
92 for (ParkingSpot spot : spots) {
93 if (spot.canFitVehicle(vehicle)) {
94 return spot;
95 }
96 }
97 return null;
98 }
99}
100
101// Parking Lot (Singleton)
102class ParkingLot {
103 private static ParkingLot instance;
104 private List<ParkingFloor> floors;
105 private Map<String, ParkingSpot> vehicleSpotMap;
106
107 private ParkingLot() {
108 floors = new ArrayList<>();
109 vehicleSpotMap = new HashMap<>();
110 }
111
112 public static synchronized ParkingLot getInstance() {
113 if (instance == null) {
114 instance = new ParkingLot();
115 }
116 return instance;
117 }
118
119 public void addFloor(ParkingFloor floor) {
120 floors.add(floor);
121 }
122
123 public boolean parkVehicle(Vehicle vehicle) {
124 for (ParkingFloor floor : floors) {
125 ParkingSpot spot = floor.findAvailableSpot(vehicle);
126 if (spot != null) {
127 spot.parkVehicle(vehicle);
128 vehicleSpotMap.put(vehicle.licensePlate, spot);
129 return true;
130 }
131 }
132 return false; // No available spot
133 }
134
135 public boolean unparkVehicle(String licensePlate) {
136 ParkingSpot spot = vehicleSpotMap.get(licensePlate);
137 if (spot != null) {
138 spot.removeVehicle();
139 vehicleSpotMap.remove(licensePlate);
140 return true;
141 }
142 return false;
143 }
144}This design uses composition (ParkingLot contains Floors, Floors contain Spots), inheritance (Vehicle hierarchy), and Singleton pattern.
Best Practices
- 1.
Start by identifying entities: ParkingLot, Floor, Spot, Vehicle
- 2.
Define clear relationships between entities
- 3.
Use enums for fixed types (vehicle types, spot types)
- 4.
Implement proper encapsulation with private fields
- 5.
Add validation in methods (canFitVehicle)
- 6.
Consider thread-safety for concurrent operations
- 7.
Add pricing strategy for calculating fees
💡 Interview Tips
Clarify requirements: floors, spot types, vehicle types, pricing
Draw class diagram showing relationships
Discuss design patterns used (Singleton, maybe Strategy for pricing)
Mention how to handle concurrency (synchronized methods, locks)
Suggest extensions: reservation system, VIP parking, electric vehicle charging
Talk about scalability: database persistence, distributed system