Design Parking System - Solution & Explanation
Problem Statement
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.
The code uses a struct with an array of size 3 to store slots available for each car type. carType is 1-indexed due to the problem constraints but is used as 0-indexed for array access. The parkingSystemAddCar function decrements the respective slot count if the slot is available. Memory is dynamically allocated and should be freed when done.
Complexity
Time Complexity: O(1) for each addCar operation as we are directly accessing an array element.
Space Complexity: O(1) as only a fixed-size array is used.
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.
This C implementation uses separate integer variables to track each type of parking slot. Each addCar operation checks the respective variable based on car type and decrements it if possible.
Complexity
Time Complexity: O(1) for checking and updating.
Space Complexity: O(1) as three variables track the state.
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).
Complexity Comparison
| Approach | Complexity |
|---|---|
| Approach 1: Use Array for Slot Management | Time Complexity: O(1) for each addCar operation as we are directly accessing an array element. |
| Approach 2: Use Separate Variables for Slot Management | Time Complexity: O(1) for checking and updating. |
| Simulation | — |
Detailed 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. |
Video Solution
Design Parking System - Leetcode 1603 - Python • NeetCodeIO • 17,274 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Design Parking System easy or hard?
How to solve Design Parking System in O(n)?
Design Parking System Python or Java solution
What is the best approach for Design Parking System?
What data structure is used in Design Parking System?
What is the time complexity of Design Parking System?
Is Design Parking System asked at Google, Amazon, or Meta?
Ready to solve this problem?
Practice Design Parking System with our built-in code editor and test cases.
Practice on FleetCode