Question Details
Problem
Design a car garage management system. The garage has multiple floors, each with a fixed number of spots. Spots are categorized: COMPACT, REGULAR, LARGE. Vehicles are categorized: MOTORCYCLE, CAR, TRUCK.
Implement the following:
python
class Garage:
def __init__(self, floors: list[list[str]]): ...
def park(self, vehicle_type: str) -> str:
**returns** spot_id or "FULL"
def leave(self, spot_id: str) -> bool:
def available_spots(self, vehicle_type: str) -> int:
A motorcycle fits in any spot. A car fits in REGULAR or LARGE. A truck fits only in LARGE.
Assignment rule: always assign the lowest-floor, lowest-index available spot that fits the vehicle.
Example
garage = Garage([["COMPACT","REGULAR","LARGE"],["REGULAR","REGULAR"]])
garage.park("CAR") -> "F0-S1"
garage.park("TRUCK") -> "F0-S2"
garage.park("CAR") -> "F1-S0"
garage.leave("F0-S1") -> True
garage.park("CAR") -> "F0-S1"
Follow-ups
- How would you support reserved spots (e.g., handicap, EV charging)?
- What changes if vehicles can be assigned to multiple contiguous spots (e.g., oversized trucks)?
- How would you track revenue if each spot type has a different hourly rate?
- Modify
parkto accept a time-of-entry andleaveto compute the fee owed.
Full Details
Problem
Design a car garage management system. The garage has multiple floors, each with a fixed number of spots. Spots are categorized: COMPACT, REGULAR, LARGE. Vehicles are categorized: MOTORCYCLE, CAR, TRUCK.
Implement the following:
python
class Garage:
def __init__(self, floors: list[list[str]]): ...
def park(self, vehicle_type: str) -> str:
**returns** spot_id or "FULL"
def leave(self, spot_id: str) -> bool:
def available_spots(self, vehicle_type: str) -> int:
A motorcycle fits in any spot. A car fits in REGULAR or LARGE. A truck fits only in LARGE.
Assignment rule: always assign the lowest-floor, lowest-index available spot that fits the vehicle.
Example
garage = Garage([["COMPACT","REGULAR","LARGE"],["REGULAR","REGULAR"]])
garage.park("CAR") -> "F0-S1"
garage.park("TRUCK") -> "F0-S2"
garage.park("CAR") -> "F1-S0"
garage.leave("F0-S1") -> True
garage.park("CAR") -> "F0-S1"
Follow-ups
- How would you support reserved spots (e.g., handicap, EV charging)?
- What changes if vehicles can be assigned to multiple contiguous spots (e.g., oversized trucks)?
- How would you track revenue if each spot type has a different hourly rate?
- Modify
parkto accept a time-of-entry andleaveto compute the fee owed.