Design Parking System
Problem
A parking lot has spaces of three fixed sizes — big, medium, and small — and the number of spaces of each size (0 to 1000) is given once to the constructor ParkingSystem(big, medium, small). Then addCar(carType) is called up to 1000 times with carType 1 = big, 2 = medium, 3 = small.
A car may only park in a space of exactly its own size. If a matching space is still free, the car takes it and the call returns true; otherwise it returns false.
ParkingSystem(1, 1, 0); addCar(1), addCar(2), addCar(3), addCar(1)[true, true, false, false]class ParkingSystem:
def __init__(self, big, medium, small):
self.slots = [big, medium, small]
def add_car(self, car_type):
if self.slots[car_type - 1] > 0:
self.slots[car_type - 1] -= 1
return True
return False
class ParkingSystem {
constructor(big, medium, small) {
this.slots = [big, medium, small];
}
addCar(carType) {
if (this.slots[carType - 1] > 0) {
this.slots[carType - 1]--;
return true;
}
return false;
}
}
class ParkingSystem {
private final int[] slots;
public ParkingSystem(int big, int medium, int small) {
slots = new int[] { big, medium, small };
}
public boolean addCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 1]--;
return true;
}
return false;
}
}
class ParkingSystem {
int slots[3];
public:
ParkingSystem(int big, int medium, int small) {
slots[0] = big; slots[1] = medium; slots[2] = small;
}
bool addCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 1]--;
return true;
}
return false;
}
};
Explanation
The key insight is that the parking lot never needs to remember which car sits in which space. A car can only use a space of exactly its own size, every space of a given size is interchangeable, and cars never leave. So the entire state collapses into three counters: how many big, medium, and small spaces are still free.
The constructor simply stores the three capacities in an array slots = [big, medium, small]. This array layout is what makes addCar a one-liner: the problem encodes the car type as 1, 2, or 3, so carType - 1 is exactly the index of the matching counter — no if/else chain over the three sizes is needed.
Each addCar(carType) looks at slots[carType - 1]. If the counter is still positive, one space of that size is free, so we decrement the counter (that space is now taken forever) and return true. If the counter has reached 0, every space of that size is occupied and we return false without changing anything.
Walking through the default example ParkingSystem(1, 1, 0) with cars [1, 2, 3, 1]: the first big car sees slots[0] = 1 > 0, parks, and drops it to 0 → true. The medium car does the same with slots[1] → true. The small car sees slots[2] = 0 → false. The second big car sees slots[0] = 0, because the first big car used the only big space → false.
Every operation touches a single array cell: one comparison and at most one decrement, so addCar is O(1) regardless of how large the capacities are. The space used is the three integers themselves, O(1) — note that we never allocate one object per parking space, which is why capacities up to 1000 (or a billion) cost nothing extra.