Design Parking System - Problem
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 ofcarTypefor the car that wants to get into the parking lot.carTypecan 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 itscarType. If there is no space available, returnfalse, else park the car in that size space and returntrue.
Input & Output
Example 1 — Basic Operations
$
Input:
operations = ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"], values = [[1,1,0], [1], [2], [3], [1]]
›
Output:
[null, true, true, false, false]
💡 Note:
Initialize parking system with 1 big space, 1 medium space, 0 small spaces. First big car parks successfully, medium car parks successfully, small car fails (no spaces), second big car fails (space occupied).
Example 2 — Multiple Spaces
$
Input:
operations = ["ParkingSystem", "addCar", "addCar", "addCar"], values = [[2,2,1], [1], [1], [2]]
›
Output:
[null, true, true, true]
💡 Note:
Initialize with 2 big, 2 medium, 1 small space. Two big cars park successfully, then one medium car parks successfully.
Example 3 — No Available Spaces
$
Input:
operations = ["ParkingSystem", "addCar", "addCar"], values = [[0,0,0], [1], [2]]
›
Output:
[null, false, false]
💡 Note:
No spaces of any type available, so all cars are rejected.
Constraints
- 1 ≤ big, medium, small ≤ 1000
- carType is 1, 2, or 3
- At most 1000 calls will be made to addCar
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Create parking system with specified space counts
2
Add Car
Check availability and park car if space exists
3
Track Usage
Update available space count for that type
Key Takeaway
🎯 Key Insight: Only track remaining space counts, not individual slot locations - this achieves O(1) efficiency
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code