Approach
The implementation in this article is done in Java. As asked in the question, we will create a constructor named ParkingSystem(big(int), medium(int), small(int)). We will create three global variables, namely big, medium, and small. The constructor will initialize the global variables with the incoming values.
We will create a function boolean addCar(carType(int)). The function will take the values one, two, or three representing the car types- big, medium, or small. On getting a valid car type, we will reduce the corresponding car parking space by one and return true. As long as there is space in our parking lot, we will add cars. If there is no space in the parking lot, we will return false.
Recommended: Try to code the Design Parking System yourself first before proceeding to the solution code.
Java implementation
//Simple Java implementation of
//Design Parking System
class ParkingSystem {
public 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) {
if(carType == 1 && big > 0){
big--;
return true;
}
if(carType == 2 && medium > 0){
medium--;
return true;
}
if(carType == 3 && small > 0){
small--;
return true;
}
return false;
}
public static void main(String[] args){
ParkingSystem obj1 =new ParkingSystem(1,2,1);
System.out.print(obj1.addCar(1) + " "); //first big car
System.out.print(obj1.addCar(2) + " "); //first medium car
System.out.print(obj1.addCar(2) + " "); //second medium car
System.out.print(obj1.addCar(3) + " "); //first small car
System.out.print(obj1.addCar(1) + " "); //second big car
System.out.print(obj1.addCar(2) + " "); //third medium car
}
}

You can also try this code with Online Java Compiler
Run Code
OUTPUT
true true true true false false
Complexities
Time Complexity
In the given implementation, simple if-else checks are used, which have a constant run time. Thus, the time complexity is, T(n) = O(1)
Space Complexity
In the given implementation, no extra space is required for the code to run. Thus, Space complexity = O(1)
Frequently Asked Questions
Can we avoid using nested IF-ELSE to solve the Design Parking System problem?
Yes, we can do so. We can create an array of size 3, and initialize the values with the incoming parking lot sizes. In the addCar() function, simply decrease the value at arr[carType] by showing that a car has been added.
Are there other variations of the Design Parking System problem?
Yes, there are a few other variations of the Design Parking System problem. One of them is keeping track of the incoming and outgoing cars and the time for which they have parked. The total time can find the amount the driver has to pay to use the parking lot.
Conclusion
To summarize the article, we thoroughly discussed the Design Parking System problem. We saw the problem statement, an example with the explanation. We also saw one of the approaches and its corresponding code in Java. Finally, we concluded with some FAQs.
Want to ace the coding rounds of big tech companies? Try our Attempt Unlimited Online Mock Test Series to start your preparation.
Learn various topics from Web Technologies, Programming Fundamentals, Data Structures, and Algorithms from our Library.
Recommended Reading:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.
You can also consider our System Design Course to give your career an edge over others.
Happy Coding!