Design Parking System
Easy
Topics
Design a parking system for a lot with three kinds of spaces: big, medium, and small, with a fixed number of each. Implement ParkingSystem(big, medium, small) and addCar(carType) where carType is 1 (big), 2 (medium), or 3 (small). Return true if there is an available slot of that type (and park the car), otherwise false.
Example 1
Cars are interchangeable, so no car ever needs storing — three counters are the entire state. Each
addCar decrements its size’s counter if any space is left. Recognising that the identities do not matter is the whole problem.Calls run top to bottom; the arrow shows what each one returns.
Input: operations = ["ParkingSystem","addCar","addCar","addCar","addCar"], values = [[1,1,0],[1],[2],[3],[1]] Output: [null,true,true,false,false]
Constraints
- 0 <= big, medium, small <= 1000
- carType is 1, 2, or 3.
- At most 1000 calls to addCar.
Editorial — intuition, approach, complexity and pitfalls
Intuition
Only the remaining count of each slot type matters. Keep three counters and decrement the right one when a car parks.
Approach
- Store counts indexed by carType (1, 2, 3).
- addCar checks the counter for that type; if positive, decrement and return true.
- Otherwise return false.
Dry Run
ParkingSystem(1, 1, 0) -- 1 big, 1 medium, 0 small
free = [0, 1, 1, 0]
^ index 0 is deliberately unused, so carType indexes straight in
addCar 1 free [0, 1, 1, 0] -> [0, 0, 1, 0] true
addCar 2 free [0, 0, 1, 0] -> [0, 0, 0, 0] true
addCar 3 free [0, 0, 0, 0] -> unchanged false -- 0 small slots existed
addCar 1 free [0, 0, 0, 0] -> unchanged false -- the big slot is taken
answer null|true|true|false|false
Two of the four calls return false for different reasons, and the counter cannot
tell them apart: type 3 never had a slot, type 1 had one and used it. Both are
simply "the count is 0", which is the point -- nothing about which car is parked
where is ever needed, only how many slots remain.
The wasted slot at index 0 is the whole trick. carType arrives as 1, 2 or 3, so a
3-element array would need free[carType - 1] on every access, and that subtraction
is exactly where an off-by-one gets introduced. One unused integer buys the
indexing for free.
Note the decrement only happens on the true path, and the order is not
interchangeable. Decrement first and keep the `== 0` test, and this run inverts
completely: false|false|true|true instead of true|true|false|false, with free
ending at [0, -1, 0, -1]. The counter stops meaning "slots remaining" the moment
it is decremented for a car that was never admitted.Complexity
- Time — O(1): each addCar is a single counter check and decrement.
- Space — O(1): three counters.
Common Pitfalls
- Index slots by carType directly (1-based) to avoid off-by-one mistakes.
Full Solution — all languages
class ParkingSystem:
def __init__(self, big, medium, small):
# index 0 unused so carType maps straight onto the slot count
self.free = [0, big, medium, small]
def addCar(self, carType):
if self.free[carType] == 0:
return False
self.free[carType] -= 1
return Trueimport java.util.*;
class ParkingSystem {
private final int[] free;
public ParkingSystem(int big, int medium, int small) {
free = new int[]{0, big, medium, small};
}
public boolean addCar(int carType) {
if (free[carType] == 0) return false;
free[carType]--;
return true;
}
}#include <bits/stdc++.h>
using namespace std;
class ParkingSystem {
public: ParkingSystem(int big, int medium, int small) : free_ {
0, big, medium, small
}
{
}
bool addCar(int carType) {
if (free_[carType] == 0) return false;
free_[carType]--;
return true;
}
private: array<int, 4> free_;
};class ParkingSystem {
constructor(big, medium, small) {
this.free = [0, big, medium, small];
}
addCar(carType) {
if (this.free[carType] === 0) return false;
this.free[carType]--;
return true;
}
}
Asked at
Tesla