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
.For example:
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.
How would you implement this class and what are the space and time complexities?
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
.A simple approach is to maintain separate counters for each parking space type (big, medium, small). The constructor initializes these counters with the given capacity. The addCar
method checks if the counter for the given carType
is greater than zero. If it is, decrement the counter and return true
. Otherwise, return false
.
The optimal solution is essentially the same as the naive solution since the problem constraints are simple and direct. We use the counters in the constructor and the addCar
method.
class ParkingSystem {
private int big;
private int medium;
private int 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) {
if (big > 0) {
big--;
return true;
} else {
return false;
}
} else if (carType == 2) {
if (medium > 0) {
medium--;
return true;
} else {
return false;
}
} else {
if (small > 0) {
small--;
return true;
} else {
return false;
}
}
}
}
addCar
: O(1)big
, medium
, or small
can be 0. The code handles this correctly as the addCar
method will return false
if the corresponding counter is 0.carType
values (e.g., less than 1 or greater than 3) are not explicitly handled in the provided code, but could be added via a conditional check that returns false
. The problem states that carType
will be 1, 2, or 3 so you would clarify this during the interview.