Design a Parking Lot
UberSoftware Engineer · OnsiteLast updated
This problem tests object-oriented design principles and the ability to manage resource allocation and concurrency in a multi-threaded environment.
Problem Statement
Design an object-oriented system for a multi-level parking lot. The system should manage parking spots of different sizes (e.g., Compact, Large, Handicapped) and handle vehicles of corresponding types.
Requirements
- The system must support multiple floors.
- Each floor has a limited number of spots of each type.
- A vehicle must be parked in a spot that is at least as large as the vehicle.
- Provide an API to park a vehicle and remove a vehicle.
- The system must be thread-safe to handle concurrent requests for spots.
Classes to Define
ParkingLot: The entry point for managing floors and overall capacity.ParkingSpot: Represents an individual space with a status (occupied/free) and a type.Vehicle: Base class/interface for different vehicle types (Car, Truck, Motorcycle).
Interface
class ParkingLot:
def park_vehicle(self, vehicle: Vehicle) -> Optional[ParkingSpot]:
# Find an available spot, mark it as occupied, and return it
pass
def remove_vehicle(self, spot: ParkingSpot) -> None:
# Free the spot
pass
Constraints
- The system should efficiently find the nearest available spot.
- Consider how to handle full parking lots gracefully.
Example
- Input:
Car()requests a spot. - Operation: The system identifies a free 'Compact' spot on the ground floor and assigns it.
- Result: The spot status changes to 'Occupied' and the system returns the
ParkingSpotobject.
Hints
Hint 1
Use an Abstract Factory or Strategy pattern to determine which spots a specific vehicle type is eligible to occupy.
Hint 2
Utilize a data structure like a thread-safe Queue or a Set for each spot type per floor to allow for constant-time spot retrieval.
Hint 3
Implement locking mechanisms or use thread-safe data structures like Python's `queue.Queue` or `threading.Lock` to ensure two threads do not claim the same spot simultaneously.
Follow-up questions
- How would you modify your design to support a 'parking reservation' feature for a specific time slot?
- If the parking lot spans multiple buildings, how would you change the system architecture to handle distributed data?
- How would you integrate a payment system that calculates the fee based on the vehicle type and duration of stay?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.