Watch 10 video solutions for Design Parking System, a easy level problem involving Design, Simulation, Counting. This walkthrough by NeetCodeIO has 16,426 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 part of the constructor.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 space available, return false, else park the car in that size space and return true.
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 <= 1000carType is 1, 2, or 31000 calls will be made to addCarProblem 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 | 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. |