# Design Parking System

Last Updated: May 13, 2022

## Introduction

Design Parking System problem is an easy difficulty question that has been asked in a few competitive coding platforms. The problem can be solved using various techniques in different languages. This article will discuss one of the most straightforward Java implementations of the Design Parking System.

## Problem Statement

In this problem, we have to design a parking system for a parking lot. The parking system can contain three types of cars- big, medium,  and small. We have to implement the class ParkingSystem with the following constraints:

• ParkingSystem(int big, int medium, int small): Constructor that takes the maximum limit of each car type that can be kept in the parking lot. Initialize the objects of the class with the incoming values.
• bool addCar(int carType): takes the type of car that needs to be added into the parking lot as an argument. The valid inputs for this function can be only one, two, and three. Any other value is not accepted. If a car can be added to the parking lot, return true. If no space is available in the parking lot, then return false.

## Example and Explanation

INPUT

ParkingSystem[1,2,1]

OUTPUT

[true, true, true, true, false, false]

In the input, we initialize the constructor with the values- [1, 2, 1]. This means that our parking lot can contain one big car, two medium cars, and one small car. We also pass the values [1, 2, 2, 3, 1, 2] to our addCar function. This means that we want to park two big cars, three medium cars and one small car in our parking lot.

The output obtained displays whether the car type can be parked or not. So, the cars that can be parked are:

• The first big car.
• The following two medium cars.
• The next small car.

The car that cannot be kept are:

• The second big car.
• The third medium car.

## 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;
}

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
}
}``````

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 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)

1. Can we avoid using nested IF-ELSE to solve the Design Parking System problem?
Ans. Yes, we can do so. We can create an array of size 3, initialize the vales 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.
2. Are there other variations of the Design Parking System problem?
Ans. 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.

## Key Takeaways

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.

Happy Coding!