Design Parking System — Counter-Based Slot Management

Sanjeev SharmaSanjeev Sharma
5 min read

Advertisement

Problem Statement

Design a parking system for a parking lot with a fixed number of big, medium, and small spaces. Implement ParkingSystem(big, medium, small) to initialise the lot and addCar(carType) to park a car of the given type (1=big, 2=medium, 3=small). Return true if the car is successfully parked, false if no space of that type remains.

Constraints:

  • 0 <= big, medium, small <= 1000
  • carType is 1, 2, or 3
  • At most 1000 calls to addCar
Input:  ParkingSystem(1,1,0), addCar(1), addCar(2), addCar(3), addCar(1)
Output: true, true, false, false
Input:  ParkingSystem(0,0,5), addCar(3), addCar(3), addCar(3)
Output: true, true, true

Why This Problem Matters

While this is an easy problem, it models a real pattern: bounded resource allocation by type. Cloud resource managers, Kubernetes pod scheduling by node type (GPU vs CPU vs memory-optimised), and hotel room booking systems all implement typed capacity tracking with exactly this structure.

Amazon asks this as a warm-up in phone screens to verify you can write clean, bug-free code under time pressure. The correct approach—using an array indexed by car type—demonstrates you understand 1-indexed vs 0-indexed arrays and prefer compact representations over verbose switch statements.

This problem also introduces the spaces[carType]-- > 0 one-liner pattern, which is a clean way to check-and-decrement atomically in interview code. Understanding this idiom signals code quality awareness.

The Core Insight

Store the three capacities in an array indexed 1 through 3: spaces = [0, big, medium, small]. Index 0 is unused, making the 1-indexed carType map directly to the array position without any offset arithmetic.

addCar(carType): if spaces[carType] == 0, return false; otherwise, decrement and return true. The one-liner spaces[carType]-- > 0 performs both the check and the decrement in a single expression—but note that Python does not have --, so use an explicit check-and-decrement.

Visual Dry Run

CallspacesResult
init(1,1,0)[0, 1, 1, 0]
addCar(1)[0, 0, 1, 0]true
addCar(2)[0, 0, 0, 0]true
addCar(3)(spaces[3]=0)false
addCar(1)(spaces[1]=0)false

Solution (Optimal)

class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.spaces = [0, big, medium, small]
 
    def addCar(self, carType: int) -> bool:
        if self.spaces[carType] == 0:
            return False
        self.spaces[carType] -= 1
        return True
class ParkingSystem {
    constructor(big, medium, small) {
        this.s = [0, big, medium, small];
    }
 
    addCar(t) {
        if (this.s[t] === 0) return false;
        this.s[t]--;
        return true;
    }
}

Time: O(1) for addCar — constant-time array lookup and decrement
Space: O(1) — fixed array of size 4 regardless of input

Common Mistakes

  • Using a 0-indexed array and doing spaces[carType - 1] — this works but adds unnecessary complexity; 1-indexed with a dummy 0th element is cleaner
  • Not checking == 0 before decrementing — decrementing below 0 would allow "negative" parking spaces
  • Using a dictionary instead of an array — &#123;1: big, 2: medium, 3: small&#125; works but is slower and more verbose for indexed access
  • Forgetting that carType is 1, 2, or 3, not 0, 1, 2 — a common off-by-one error when switching between 0-indexed and 1-indexed thinking
  • Returning the current count instead of a boolean — the method must return true/false, not the remaining spaces

Interview Tips

  • Use the 1-indexed array trick as your opening move—it shows elegance under simplicity
  • Mention that the spaces[carType]-- > 0 one-liner in C++/Java is clean but evaluate whether the interviewer prefers explicit two-step code for readability
  • For a small problem like this, the interview focus is on code quality: correct return type, no magic numbers, clear variable names
  • If asked to extend this to N car types, note that the array approach generalises to spaces = [0] + [count_per_type] for arbitrary type counts

Follow-up Questions

  • How would you support querying the number of remaining spaces by type? (Add a getRemaining(carType) method returning spaces[carType])
  • How would you handle a parking lot that accepts multiple car types per space (e.g., small cars can use big spaces)? (Add fallback logic: if spaces[carType] == 0, check next larger type)
  • How would you scale this to a multi-floor parking garage with floors having different capacities? (2D array indexed by [floor][carType])
  • How would you support concurrent addCar calls from multiple threads? (Atomic decrement with compare-and-swap; or a mutex per car type)
  • How would you track which specific spots are occupied (not just counts)? (Maintain a queue of available spot IDs per type; dequeue on addCar, enqueue on removeCar)

Key Takeaways

  • Use a 1-indexed array [0, big, medium, small] so carType maps directly to the array index
  • addCar is O(1): check if spaces[carType] > 0, decrement, return true/false
  • The dummy 0 at index 0 eliminates the need for offset arithmetic when carType is 1-indexed
  • Always guard the decrement with a zero-check to prevent negative space counts
  • This models the general typed resource allocation pattern used in Kubernetes scheduling and cloud resource pools
  • The same structure extends to N car types with spaces = [0] + list_of_capacities
  • For concurrent access, use atomic decrement (CAS) or per-type locks rather than a global lock

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro

Related reading