Design Parking System solution and explanation
9 more videos on this problem
Design Parking System - Leetcode 1603 - PythonNeetCodeIO · 3:11 · 17K views
design parking system | design parking system leetcode | leetcode 1603Naresh Gupta · 4:21 · 5K views
Design Parking System | GOOGLE | Leetcode-1603 | Live CodecodestorywithMIK · 7:26 · 5K views
LEETCODE SOLUTION 1603 | Design Parking System | EASY | JavascriptCodingForBeginners · 13:22 · 1K views
1603. Design Parking System (Leetcode Easy)Programming Live with Larry · 3:22 · 980 views
1603 Design Parking System (Leetcode) | Easy SolutionPNT Coding · 7:47 · 861 views
Design Parking System - Leetcode 1603 - PythonTechError · 5:29 · 773 views
Design Parking System (Solved with 2 Approaches)| LEETCODE - 1603 | Easy solutionhi-malik · 7:04 · 724 views
Design Parking System - LeetCode #1603 Python, JavaScript, Java and C++CodingNinja · 3:50 · 678 views
Leetcode 1603 Design Parking System (Java) - Simplifying 22 lines to 1 lineTheAnalyticGuy · 7:10 · 625 views
The problem
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 theParkingSystemclass. The number of slots for each parking space are given as part of the constructor.bool addCar(int carType)Checks whether there is a parking space ofcarTypefor the car that wants to get into the parking lot.carTypecan be of three kinds: big, medium, or small, which are represented by1,2, and3respectively. A car can only park in a parking space of itscarType. If there is no space available, returnfalse, else park the car in that size space and returntrue.
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. It is already occupied.
Constraints:
0 <= big, medium, small <= 1000carTypeis1,2, or3- At most
1000calls will be made toaddCar
Approach overview
Problem Overview: You need to design a parking system that manages three types of slots: big, medium, and small. Each call to addCar(carType) attempts to park a car of the given type and should return true if a slot is available or false otherwise.
This is a classic design and simulation problem. Instead of searching through parking spots, the key observation is that you only need to track how many spaces remain for each car type. Every operation becomes a simple counter update.
Approach 1: Use Array for Slot Management (Time: O(1), Space: O(1))
Store the number of available slots for each car type in an array of size 3. Map car types directly to indices: 1 → big, 2 → medium, 3 → small. During initialization, fill the array with the provided capacities. When addCar is called, check the corresponding index in the array. If the value is greater than zero, decrement it and return true; otherwise return false. This approach works because every operation becomes a constant-time lookup and update, which fits perfectly with counting-based problems where you only track remaining capacity rather than individual items.
Approach 2: Use Separate Variables for Slot Management (Time: O(1), Space: O(1))
Instead of an array, maintain three integer variables: big, medium, and small. The constructor initializes these variables with the provided slot counts. In the addCar method, use conditional checks on carType. If the matching variable is positive, decrement it and return true. Otherwise return false. This approach is slightly more explicit and avoids indexing logic, which some developers find easier to read during interviews.
The main difference between the two approaches is code structure. The array version is compact and scales easily if more slot types are added. The variable-based version is straightforward and often preferred for quick implementation.
Recommended for interviews: Both approaches achieve optimal O(1) time and O(1) space complexity, which is exactly what interviewers expect for this problem. The array approach demonstrates clean abstraction and extensibility, while the separate variable approach shows clarity and simplicity. Showing either solution quickly and explaining why constant-time operations are sufficient signals strong understanding of system state management.
Approach 1: Approach 1: Use Array for Slot Management
This approach leverages an array to manage the count of available parking slots for each type of car. The array indices correspond to the car types: index 0 for big cars, index 1 for medium, and index 2 for small. The addCar function decrements the corresponding index if space is available.
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.slots = [big, medium, small]
def addCar(self, carType: int) -> bool:
if self.slots[carType - 1] > 0:
self.slots[carType - 1] -= 1
return True
return Falseclass ParkingSystem {
private 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;
}
}#include <vector>
class ParkingSystem {
std::vector<int> slots;
public:
ParkingSystem(int big, int medium, int small) : slots({big, medium, small}) {}
bool addCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 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;
}
}#include <stdbool.h>
typedef struct {
int slots[3];
} ParkingSystem;
ParkingSystem* parkingSystemCreate(int big, int medium, int small) {
ParkingSystem* obj = (ParkingSystem*) malloc(sizeof(ParkingSystem));
obj->slots[0] = big;
obj->slots[1] = medium;
obj->slots[2] = small;
return obj;
}
bool parkingSystemAddCar(ParkingSystem* obj, int carType) {
if (obj->slots[carType - 1] > 0) {
obj->slots[carType - 1]--;
return true;
} else {
return false;
}
}
void parkingSystemFree(ParkingSystem* obj) {
free(obj);
}public class ParkingSystem {
private int[] slots;
public ParkingSystem(int big, int medium, int small) {
slots = new int[] {big, medium, small};
}
public bool AddCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 1]--;
return true;
}
return false;
}
}The Python implementation uses a list to track the available parking slots for each car type. The addCar method updates the count for the corresponding slot, using list indexing to access the correct slot.
Time Complexity: O(1) per operation.
Space Complexity: O(1).
Approach 2: Approach 2: Use Separate Variables for Slot Management
This method uses distinct variables to handle each type of car's parking slots. This approach makes the code very clear for small data sets. While this isn't necessarily more efficient than the array method for this particular problem, it offers an alternative for simple systems where explicit clarity is beneficial.
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) -> bool:
if carType == 1:
if self.big > 0: self.big -= 1; return True
elif carType == 2:
if self.medium > 0: self.medium -= 1; return True
elif carType == 3:
if self.small > 0: self.small -= 1; return True
return Falseclass ParkingSystem {
private int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
switch (carType) {
case 1:
if (big > 0) { big--; return true; }
break;
case 2:
if (medium > 0) { medium--; return true; }
break;
case 3:
if (small > 0) { small--; return true; }
break;
}
return false;
}
}class ParkingSystem {
int big, medium, small;
public:
ParkingSystem(int big, int medium, int small) : big(big), medium(medium), small(small) {}
bool addCar(int carType) {
if (carType == 1) {
if (big > 0) { big--; return true; }
} else if (carType == 2) {
if (medium > 0) { medium--; return true; }
} else if (carType == 3) {
if (small > 0) { small--; return true; }
}
return false;
}
};class ParkingSystem {
constructor(big, medium, small) {
this.big = big;
this.medium = medium;
this.small = small;
}
addCar(carType) {
if (carType === 1) {
if (this.big > 0) { this.big--; return true; }
} else if (carType === 2) {
if (this.medium > 0) { this.medium--; return true; }
} else if (carType === 3) {
if (this.small > 0) { this.small--; return true; }
}
return false;
}
}#include <stdbool.h>
typedef struct {
int big;
int medium;
int small;
} ParkingSystem;
ParkingSystem* parkingSystemCreate(int big, int medium, int small) {
ParkingSystem* obj = (ParkingSystem*) malloc(sizeof(ParkingSystem));
obj->big = big;
obj->medium = medium;
obj->small = small;
return obj;
}
bool parkingSystemAddCar(ParkingSystem* obj, int carType) {
if (carType == 1 && obj->big > 0) {
obj->big--;
return true;
} else if (carType == 2 && obj->medium > 0) {
obj->medium--;
return true;
} else if (carType == 3 && obj->small > 0) {
obj->small--;
return true;
}
return false;
}
void parkingSystemFree(ParkingSystem* obj) {
free(obj);
}public class ParkingSystem {
private int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public bool AddCar(int carType) {
switch (carType) {
case 1:
if (big > 0) { big--; return true; }
break;
case 2:
if (medium > 0) { medium--; return true; }
break;
case 3:
if (small > 0) { small--; return true; }
break;
}
return false;
}
}This Python variant uses distinct attributes for managing parking spaces. Using conditional statements, addCar checks and updates respective attribute values.
Time Complexity: O(1).
Space Complexity: O(1).
Approach 3: Simulation
We use an array cnt of length 4 to represent the number of parking spaces for each type of car, where cnt[1], cnt[2], and cnt[3] represent the number of large, medium, and small parking spaces, respectively.
During initialization, we set cnt[1], cnt[2], and cnt[3] to the number of large, medium, and small parking spaces, respectively.
Each time a car parks, we check if there is a corresponding parking space in the parking lot. If not, we return false; otherwise, we decrement the number of corresponding parking spaces by one and return true.
The time complexity is O(1), and the space complexity is O(1).
class ParkingSystem {
private cnt: [number, number, number, number];
constructor(big: number, medium: number, small: number) {
this.cnt = [0, big, medium, small];
}
addCar(carType: number): boolean {
if (this.cnt[carType] === 0) {
return false;
}
this.cnt[carType]--;
return true;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = new ParkingSystem(big, medium, small)
* var param_1 = obj.addCar(carType)
*/type ParkingSystem struct {
cnt []int
}
func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{[]int{0, big, medium, small}}
}
func (this *ParkingSystem) AddCar(carType int) bool {
if this.cnt[carType] == 0 {
return false
}
this.cnt[carType]--
return true
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/struct ParkingSystem {
cnt: [i32; 4]
}
impl ParkingSystem {
fn new(big: i32, medium: i32, small: i32) -> Self {
ParkingSystem {
cnt: [0, big, medium, small],
}
}
fn add_car(&mut self, car_type: i32) -> bool {
if self.cnt[car_type as usize] == 0 {
return false;
}
self.cnt[car_type as usize] -= 1;
true
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Approach 1: Use Array for Slot Management | Time Complexity: O(1) per operation. |
| Approach 2: Use Separate Variables for Slot Management | Time Complexity: O(1). |
| Simulation | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Array for Slot Management | O(1) | O(1) | Clean mapping of car type to index. Easy to extend if more parking categories are introduced. |
| Separate Variables for Slot Management | O(1) | O(1) | Simplest implementation with explicit variables. Good for interviews where readability matters. |
Frequently asked questions
Is Design Parking System easy or hard?
Design Parking System is classified as an Easy problem on LeetCode with a high acceptance rate of over 80%. The challenge focuses on modeling a simple system and maintaining counters correctly rather than implementing complex algorithms.
How to solve Design Parking System in O(n)?
An O(n) solution is unnecessary for this problem. The optimal design tracks the number of available slots for each car type and updates the count when a car parks. With this approach, each operation runs in O(1) time because it only involves a simple counter check and decrement.
Design Parking System Python or Java solution
In Python or Java, the typical solution stores the remaining slot counts in either an array or class variables. The constructor initializes these counts, and the addCar method checks the requested type and decreases the count if space is available. Each call runs in O(1) time.
What is the best approach for Design Parking System?
The best approach is to store the remaining capacity of each parking type and update it during every addCar call. Using either an array of size three or three separate variables allows constant-time checks and updates. Both implementations achieve O(1) time and O(1) space complexity, which is optimal for this design problem.
What data structure is used in Design Parking System?
The problem typically uses simple counting with either an array or individual integer variables. An array maps car types to indices, while separate variables directly represent big, medium, and small slot counts. Both approaches rely on constant-time updates.
What is the time complexity of Design Parking System?
The time complexity is O(1) per operation. Each addCar call performs a direct lookup of the remaining slot count and optionally decrements it. No iteration or search is required, so the operation always runs in constant time.
Is Design Parking System asked at Google, Amazon, or Meta?
Design-style problems like this frequently appear in interviews at large tech companies including Amazon and Google. They test whether you can model a system, maintain state correctly, and implement efficient constant-time operations.
