educative.io

How does the parking lot keep track of the free spaces for each kind of parking spot?

In the DisplayBoard class, I see that it contains:
private Handicapped handicappedSpot;
private Compact compactSpot;
private Large largeSpot;
private MotorCycle motorCycleSpot;

However these are single parking spots and don’t really contain information about the number of free spots for the entire parking lot? Shouldn’t the number of free spots be kept track as part of parking lot, which is designed as a singleton pattern for the whole system? Thank you in advance for any clarification you can provide. Thanks


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/7D6XVRqxXVj

Hi @Yunfei_Xie,

As currently implemented, you are correct that the DisplayBoard class only contains information about the availability of individual parking spots rather than the overall availability of the entire parking lot.

To keep track of the number of free spots for the entire parking lot, it would be better to include a separate data structure in the ParkingLot class that keeps track of the number of free spots for each type of parking spot (e.g. handicapped, compact, large, motorcycle). This data structure could be updated each time a vehicle enters or exits the parking lot, and the DisplayBoard could then query the ParkingLot class for this information and display it on the display board.

Happy Learning :slight_smile:

Thank you for your answer! Can DisplayBoard follow the Observer pattern? Entrance and Exit can notify the ParkingLot when a vehicle enters or exits and then ParkingLot notifies the DisplayBoard?


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/7D6XVRqxXVj

Yes, you are right, the DisplayBoard class can indeed follow the Observer pattern in the parking lot system. This approach would allow the DisplayBoard class to be notified of any changes in the parking lot, without having to actively query the parking lot for updates. This would make the system more efficient and reduce unnecessary processing overhead.

1 Like