Design Parking System
EasyPrompt
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
Approaches
2 approaches with complexity analysis and trade-offs.
This approach uses separate instance variables to store the available slots for each car type (big, medium, and small). The addCar method then uses a series of if-else statements to check the carType and update the corresponding slot count.
Algorithm
- Initialize three integer member variables in the constructor:
bigSlots,mediumSlots, andsmallSlots. - In the
addCarmethod, use a series ofif-else ifstatements to determine thecarType. - For
carType == 1, check ifbigSlotsis greater than 0. If it is, decrementbigSlotsand returntrue. - For
carType == 2, check ifmediumSlotsis greater than 0. If it is, decrementmediumSlotsand returntrue. - For
carType == 3, check ifsmallSlotsis greater than 0. If it is, decrementsmallSlotsand returntrue. - If the respective slot count is 0 for the given
carType, returnfalse.
Walkthrough
In this straightforward approach, we declare three integer member variables in the ParkingSystem class: one for each car type (big, medium, small). The constructor's role is to initialize these three variables with the provided initial counts.
The addCar(int carType) method contains the main logic. It uses a conditional block (an if-else if-else structure) to handle the different car types:
- It first checks if
carTypeis 1 (for a big car). - If it is, it checks if there are any available big slots (
bigSlots > 0). - If a slot is available, it decrements the count of big slots and returns
true. - If no slot is available, it returns
false. - The same logic is repeated for medium (
carType == 2) and small (carType == 3) cars, using their respective counter variables.
While this method is very explicit and easy to understand for a fixed set of three car types, it becomes less elegant and harder to maintain if the number of types were to increase.
class ParkingSystem { private int bigSlots; private int mediumSlots; private int smallSlots; public ParkingSystem(int big, int medium, int small) { this.bigSlots = big; this.mediumSlots = medium; this.smallSlots = small; } public boolean addCar(int carType) { if (carType == 1) { if (this.bigSlots > 0) { this.bigSlots--; return true; } } else if (carType == 2) { if (this.mediumSlots > 0) { this.mediumSlots--; return true; } } else if (carType == 3) { if (this.smallSlots > 0) { this.smallSlots--; return true; } } return false; }}Complexity
Time
O(1). Both the constructor and the `addCar` method perform a constant number of operations (assignments and comparisons), resulting in constant time complexity.
Space
O(1). The space required is constant as it only uses three integer variables to store the slot counts, regardless of the number of cars or calls to `addCar`.
Trade-offs
Pros
Extremely simple to implement and understand for the given problem constraints.
No overhead from using more complex data structures.
Cons
The code is verbose and contains repetitive logic.
It is not easily scalable. Adding a new car type would require adding another branch to the
if-elsestructure, modifying the core logic of theaddCarmethod.
Solutions
Solution
public class ParkingSystem { private List < int > cnt ; public ParkingSystem ( int big , int medium , int small ) { cnt = new List < int >() { 0 , big , medium , small }; } public bool AddCar ( int carType ) { if ( cnt [ carType ] == 0 ) { return false ; } -- cnt [ carType ]; return true ; } } /** * Your ParkingSystem object will be instantiated and called as such: * ParkingSystem obj = new ParkingSystem(big, medium, small); * bool param_1 = obj.AddCar(carType); */Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.