Back to blog
Jun 12, 2025
4 min read

Design Parking System

Design a parking system that manages parking slots for big, medium, and small cars, checking availability upon entry.

Difficulty: Easy | Acceptance: 87.20% | Paid: No Topics: Design, Simulation, Counting

Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as arguments. bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no available space, return false, else park the car in that size space and return true.

Examples

Example 1:

Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]

Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. Slot 1 is taken already.

Constraints

0 <= big, medium, small <= 1000
carType is 1, 2, or 3
At most 1000 calls will be made to addCar

Approach 1: Instance Variables

Intuition Store the remaining capacity for each car type in three separate integer member variables. This provides the most direct and fastest access without any indexing overhead.

Steps

  • Initialize three variables (big, medium, small) in the constructor with the given capacities.
  • In the addCar method, use a conditional statement (if-else or switch) to check the carType.
  • If the corresponding variable is greater than 0, decrement it and return true.
  • Otherwise, return false.
python
class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.big = big
        self.medium = medium
        self.small = small

    def addCar(self, carType: int) -&gt; bool:
        if carType == 1:
            if self.big &gt; 0:
                self.big -= 1
                return True
        elif carType == 2:
            if self.medium &gt; 0:
                self.medium -= 1
                return True
        elif carType == 3:
            if self.small &gt; 0:
                self.small -= 1
                return True
        return False

Complexity

  • Time: O(1) - Constant time checks and updates.
  • Space: O(1) - Fixed space for three integers.
  • Notes: Fastest execution due to lack of array overhead.

Approach 2: Array Indexing

Intuition Use an array (or list) to store the parking slots. The carType (1, 2, 3) maps directly to the array index (0, 1, 2) by subtracting 1. This reduces code duplication.

Steps

  • Initialize an array of size 3 with the given capacities.
  • In addCar, calculate the index as carType - 1.
  • Check if the value at that index is greater than 0.
  • If yes, decrement the value and return true; otherwise, return false.
python
class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.slots = [big, medium, small]

    def addCar(self, carType: int) -&gt; bool:
        idx = carType - 1
        if self.slots[idx] &gt; 0:
            self.slots[idx] -= 1
            return True
        return False

Complexity

  • Time: O(1) - Array access by index is constant time.
  • Space: O(1) - Fixed size array of 3 elements.
  • Notes: Cleaner code structure compared to separate variables.

Approach 3: Hash Map

Intuition Use a hash map (dictionary) to store the remaining slots as key-value pairs. This is a generic design pattern that easily accommodates adding more car types in the future without changing the logic structure.

Steps

  • Initialize a map with keys 1, 2, 3 and their respective capacities.
  • In addCar, retrieve the value for the given carType key.
  • If the value exists and is greater than 0, decrement it and return true.
  • Otherwise, return false.
python
class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.slots = {1: big, 2: medium, 3: small}

    def addCar(self, carType: int) -&gt; bool:
        if self.slots.get(carType, 0) &gt; 0:
            self.slots[carType] -= 1
            return True
        return False

Complexity

  • Time: O(1) - Hash map operations are average O(1).
  • Space: O(1) - Stores only 3 key-value pairs.
  • Notes: More flexible for future extensions but slightly higher constant factor overhead than arrays or variables.