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

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
Parking System Design OverviewInitial State: ParkingSystem(1,1,0)Big1Medium1Small0Operations: addCar(1) → addCar(2) → addCar(3) → addCar(1)After Operations:Big0Medium0Small0Results:✅ true✅ true❌ false❌ falseOutput: [null, true, true, false, false]
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
Asked in
Amazon 25 Google 15 Microsoft 12
89.0K Views
Medium Frequency
~10 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen