Design Parking System · Optimal Solution

Three counters. One decision per car.

A multi-level parking garage reserves separate floors for big vehicles, medium sedans, and compact cars — each with a fixed number of spots. When a vehicle arrives, the attendant checks that floor's counter: if a spot remains, park and decrement; otherwise, turn the vehicle away. Three counters, one decrement per arrival, one comparison per query. O(1) per operation.


Interactive

Watch the algorithm run


Concept

Independent Buckets

Each car type maps to exactly one counter. A big car cannot take a medium slot. The system is three independent decrement-or-reject counters — no cross-type borrowing.

The common mistake is using a boolean "has space" flag instead of an integer count. A boolean forgets that capacity can be greater than one.


Algorithm

Algorithm

Three independent counters, each decremented on arrival. No search, no sorting — just an array lookup and a decrement.

  1. 1

    Initialize counters

    Store three counters — big, medium, small — set to the capacity of each lot.

  2. 2

    Receive a car

    On each addCar(type) call, check the counter for the given type.

  3. 3

    Accept or reject

    If the counter is positive, decrement it and return true. Otherwise return false.


Edge Cases

Edge Patterns

Understanding boundary behavior is as important as the main algorithm path.

Zero initial capacity

A lot that starts at capacity 0 rejects every car of that type immediately.

Mixed availability

One lot may be full while others have space. Each type is tracked independently.


Complexity

Complexity

Time: O(1) per AddCar — single array lookup and decrement.
Space: O(1) — three integer counters.