InterviewDB Question

Car Garage - OOD Parking System Design

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

  1. How would you support reserved spots (e.g., handicap, EV charging)?
  2. What changes if vehicles can be assigned to multiple contiguous spots (e.g., oversized trucks)?
  3. How would you track revenue if each spot type has a different hourly rate?
  4. Modify park to accept a time-of-entry and leave to 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

  1. How would you support reserved spots (e.g., handicap, EV charging)?
  2. What changes if vehicles can be assigned to multiple contiguous spots (e.g., oversized trucks)?
  3. How would you track revenue if each spot type has a different hourly rate?
  4. Modify park to accept a time-of-entry and leave to compute the fee owed.
Free preview. Unlock all questions →

Topics

Coding Onsite