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 <= 1000
carType
is 1
, 2
, or 3
1000
calls will be made to addCar
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The parking system has a limited number of spots for each car size. The brute force approach simulates every possible parking attempt to see if there's space for a given car.
Here's how the algorithm would work step-by-step:
class ParkingSystem:
def __init__(self, big_spots: int, medium_spots: int, small_spots: int):
self.available_big_spots = big_spots
self.available_medium_spots = medium_spots
self.available_small_spots = small_spots
def addCar(self, car_type: int) -> bool:
# Check for big cars
if car_type == 1:
if self.available_big_spots > 0:
self.available_big_spots -= 1
return True
else:
return False
# Check for medium cars
elif car_type == 2:
if self.available_medium_spots > 0:
self.available_medium_spots -= 1
return True
else:
return False
# Check for small cars
elif car_type == 3:
# Only proceed if there's space
if self.available_small_spots > 0:
self.available_small_spots -= 1
return True
else:
return False
return False
The design parking system problem can be solved efficiently by keeping track of the number of parking spots available for each car type. We simply decrement the count for the corresponding car type when a car parks and check to make sure there's space before doing so.
Here's how the algorithm would work step-by-step:
class ParkingSystem:
def __init__(self, big_spots: int, medium_spots: int, small_spots: int):
self.available_big_spots = big_spots
self.available_medium_spots = medium_spots
self.available_small_spots = small_spots
def addCar(self, car_type: int) -> bool:
# Check the car type and corresponding spot availability
if car_type == 1:
if self.available_big_spots > 0:
self.available_big_spots -= 1
return True
else:
return False
elif car_type == 2:
if self.available_medium_spots > 0:
self.available_medium_spots -= 1
return True
else:
return False
else:
# Handle small car parking
if self.available_small_spots > 0:
# Decrement the count and return true
self.available_small_spots -= 1
return True
else:
# Indicate parking is unavailable
return False
Case | How to Handle |
---|---|
Initial number of slots for a car type is negative | Throw an IllegalArgumentException as the number of slots must be non-negative. |
Car type is outside the valid range (1, 2, 3) | Throw an IllegalArgumentException as the car type must be within the defined range. |
addCar called with null or invalid car type | Throw an IllegalArgumentException if the car type is null or outside the valid range. |
Multiple calls to addCar deplete a type's slots to negative | The addCar method should return false if no slots are available and the count becomes negative. |
Integer overflow when adding cars of the same type repeatedly | The internal counters should be carefully checked for integer overflow and prevent addition if it occurs. |
Maximum number of addCar calls exceeding memory limits | The initial number of slots should be chosen such that memory usage from many addCar calls stays within reasonable limits. |
All slots are initialized to zero | The addCar method should always return false as there are no initial spots. |
Very large initial slot sizes provided | Consider potential memory usage and performance impact of extremely large initial sizes, possibly limiting the maximum allowed size to prevent resource exhaustion. |