I tried my first low-level design problem
Arya veer singh chauhan
SDE@TATA 1mg | 2077 on Leetcode (Knight) | BITS Pilani '24 | Solving problems using engineering skills
Parking Lot System design is one of the most commonly asked low-level design problems. Although I learned about design patterns in my Object-Oriented Programming course in college, implementing them in a real-world problem was itself a challenge.
I started with a basic problem statement:
Design a parking lot where multiple types of vehicles can be parked. My design uniquely provides different parking methods at runtime. Let me explain this a bit more.
One parking facility may allow only a car in a car spot, a bike in a bike spot, and a truck in a truck spot. Other facilities may permit 2-3 bikes to be parked in a car spot. This can be solved by defining the size of each vehicle.
To allow these configurations, I used the Strategy Design Pattern. At runtime, we can configure our software with what kind of parking system we want. Also, if required, other strategies can be added to the system, making it extensible.
Additionally, multiple requests can come to book one spot. I used the double-locking technique to solve the synchronization issues.
public boolean occupy(Vehicle vehicle,ParkingSpotType parkingSpotType) {
if(! this.canBook(vehicle,parkingSpotType)) return false;
this.lock.lock();
if(! this.canBook(vehicle,parkingSpotType)) {
this.lock.unlock();
return false;
}
this.occupied = true;
this.occupied_at = Timer.getTimestamp();
this.lock.unlock();
return true;
}
Attaching link to my solution here. Please help me with any mistakes I have made or anything I could have improved upon.