Parking Garage OOP Design – identify classes/objects, implement nearest parking spot logic

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-03-10

Problem Statement

Design a Parking Garage system that manages parking for multiple vehicles. The system should allow vehicles to enter, exit, and find the nearest available parking spot. The focus is on object-oriented design, modularity, and implementable algorithms for spot allocation.


Functional Requirements


1.Vehicle Entry
•Vehicles enter the garage and are assigned the nearest available parking spot.
•Update parking spot occupancy status.
2.Vehicle Exit
•Vehicles leave the garage.
•Release the parking spot and make it available for new arrivals.
3.Nearest Spot Allocation
•Implement logic to find the nearest available spot based on distance from the entrance.
•Optimize for minimal search time.
4.Parking Spot Management
•Support multiple floors and multiple types of spots (e.g., compact, regular, handicapped, electric).
•Track occupied and free spots efficiently.
5.Optional Features
•Display available spots by floor or type.
•Calculate parking fees based on time and spot type.
•Support reservations or VIP spots.


Non-Functional Requirements


•Extensibility: Easy to add new spot types, floors, or pricing rules.
•Efficiency: Quickly find nearest available spot even for large garages.
•Thread Safety: Handle concurrent vehicle entry/exit if simulated in a multi-threaded environment.


Edge Cases & Constraints


•Full garage → handle gracefully with appropriate messages.
•Vehicles leaving from wrong or invalid spots → error handling.
•Different vehicle types → assign only suitable spot type.


Design Hints


•Key Classes / Objects:
•Garage → represents the entire parking structure; manages floors and spots.
•Floor → contains multiple ParkingSpot objects.
•ParkingSpot → represents a spot, has attributes like spotId, spotType, isOccupied, distanceFromEntrance.
•Vehicle → represents vehicle information (type, license plate, entry time).
•ParkingService → main API for vehicle entry, exit, and nearest spot allocation.
•Data Structures:
•Min-Heap / PriorityQueue → to quickly retrieve the nearest available spot based on distance.
•HashMap<SpotId, ParkingSpot> → to track occupied spots and allow fast lookup for exit.
•Design Patterns:
•Strategy Pattern → for different spot allocation strategies (nearest, reserved, VIP).
•Observer Pattern (Optional) → to notify users or display boards about spot availability.


Example Flow


1.Vehicle enters → ParkingService.findNearestSpot(vehicle) → returns ParkingSpot101.
2.Spot marked as occupied → vehicle parked.
3.Vehicle exits → ParkingService.releaseSpot(spotId) → updates availability.
4.Next vehicle enters → nearest free spot retrieved from priority queue.


Evaluation Areas


•Correct class design and separation of concerns.
•Efficient nearest spot allocation.
•Handling edge cases and concurrency.
•Extensibility to add floors, spot types, or reservations.


Solution

Comments