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
Design Parking System INPUT Parking Lot Initialization BIG slots: 1 MED slots: 1 SML slots: 0 ParkingSystem(1,1,0) Operations: 1. ParkingSystem(1,1,0) 2. addCar(1) // Big car 3. addCar(2) // Medium car 4. addCar(3) // Small car 5. addCar(1) // Big car carType: 1=big, 2=med, 3=small ALGORITHM STEPS 1 Initialize Counters Store capacity for each type B:1 M:1 S:0 2 addCar(1): Check Big B:1 > 0? YES --> B:0 B:0 true 3 addCar(2): Check Med M:1 > 0? YES --> M:0 M:0 true 4 addCar(3): Check Small S:0 > 0? NO --> reject S:0 false 5 addCar(1): Check Big B:0 > 0? NO --> reject B:0 false FINAL RESULT Final Parking State BIG FULL 0/1 MED FULL 0/1 SML N/A 0/0 Output Array: null true true false false Operation Summary: 1. Init: slots set [1,1,0] 2. Big car parked - OK 3. Med car parked - OK 4. Small car - NO SPACE 5. Big car - NO SPACE Key Insight: Counter-Based Tracking Use simple integer counters for each parking type. When addCar() is called, check if counter > 0. If yes, decrement counter and return true. If no, return false. Time: O(1), Space: O(1). Each car type maps to its own counter: 1 --> big, 2 --> medium, 3 --> small. No complex data structures needed! TutorialsPoint - Design Parking System | Counter-Based Tracking Approach
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