KatInterview

Object-Oriented Design for a Multi-Floor Garage Allocator

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designconcurrencyheap

Last updated

Design classes and an allocation API for a multi-floor garage that assigns the closest suitable bay to each vehicle, stays correct under concurrent requests, and handles a full garage.

Multi-Floor Garage Allocator

You are asked to model a garage with several floors. Each floor contains a fixed number of bays in a few sizes (for example motorcycle, compact, large, and accessible). Vehicles arrive and leave at any time, from many threads at once. Your job is to design the classes and the allocation logic.

Requirements

  1. The garage has multiple floors, and each floor has a limited number of bays of each type.
  2. A vehicle may only use a bay that is at least as large as the vehicle.
  3. Provide an operation to park a vehicle and an operation to release a bay.
  4. Concurrent requests must never assign the same bay to two vehicles.
  5. Assignment should favor the closest free bay (lower floor first, then lower position on the floor).
  6. A full garage must be handled gracefully, without crashing.

Classes to define

  • ParkingLot: owns the floors and performs allocation.
  • ParkingSpot: one bay, with a type, a location, and an occupied/free state.
  • Vehicle: a base type with subclasses such as Car, Truck, and Motorcycle.

Interface

class ParkingLot:
    def park_vehicle(self, vehicle: Vehicle) -> Optional[ParkingSpot]:
        """Pick a suitable free bay, mark it occupied, and return it.
        Return None if nothing suitable is free."""

    def remove_vehicle(self, spot: ParkingSpot) -> None:
        """Mark the bay free so it can be reused."""

Example

A Car asks for a bay. The garage finds the closest free compact bay on the lowest floor, marks it occupied, and returns that ParkingSpot.

What to cover in your answer

  • The class design and where each responsibility lives.
  • How you find the nearest suitable bay quickly.
  • How you guarantee thread safety, and what could go wrong.
  • What happens when the garage, or one bay type, is full.
  • How easily a new vehicle type could be added.

Hints

Hint 1

Decide who knows what: should the vehicle declare which bay types it can use, or should the garage hard-code that mapping? Think about what happens when a new vehicle type is added.

Hint 2

For fast nearest-bay lookup, keep the free bays for each type in a priority structure ordered by (floor, position) instead of scanning every floor.

Hint 3

For concurrency, think about what must be atomic: removing a bay from the free pool and marking it occupied. Consider using a separate lock per bay type, and what bug appears if the same bay is released twice.