Design Parking System - Problem
Design a Smart Parking System

You're tasked with creating a digital parking management system for a modern parking lot. This lot has three types of parking spaces: big (for trucks and SUVs), medium (for standard cars), and small (for compact cars).

Your system needs to:
• Initialize with a fixed number of slots for each parking space size
• Process incoming cars and determine if they can park
• Track available spaces in real-time

Class Requirements:
ParkingSystem(int big, int medium, int small) - Initialize the system with the number of slots for each size
bool addCar(int carType) - Try to park a car where carType is 1 (big), 2 (medium), or 3 (small)

Return true if the car can park successfully, false if no space is available for that car type.

Input & Output

example_1.py — Basic Usage
$ Input: ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"] [[1, 1, 0], [1], [2], [3], [1]]
Output: [null, true, true, false, false]
💡 Note: Initialize with 1 big, 1 medium, 0 small spaces. First big car parks (true), medium car parks (true), small car can't park (false), second big car can't park (false).
example_2.py — Full Capacity Test
$ Input: ["ParkingSystem", "addCar", "addCar", "addCar", "addCar", "addCar"] [[2, 2, 1], [1], [1], [2], [2], [3]]
Output: [null, true, true, true, true, true]
💡 Note: Initialize with 2 big, 2 medium, 1 small space. All cars can park as we have exact capacity: 2 big cars, 2 medium cars, 1 small car.
example_3.py — Edge Case - Zero Spaces
$ Input: ["ParkingSystem", "addCar", "addCar"] [[0, 0, 1], [1], [3]]
Output: [null, false, true]
💡 Note: No big or medium spaces available. Big car can't park (false), but small car can park in the single small space (true).

Visualization

Tap to expand
Smart Parking System - Real-time Counter ManagementParking Lot LayoutBIG SPACES🚛 🚛MEDIUM SPACES🚗 🚗 🚗SMALL SPACES🚙Digital Control SystemBIG2MED3SMALL1Medium car arrives!⚡ O(1) Time | 💾 O(1) Space | 🎯 Always Optimal
Understanding the Visualization
1
System Initialization
Set up counters: Big=2, Medium=3, Small=1 spaces available
2
Car Arrives
Medium car (type 2) requests parking space
3
Instant Check
System checks medium counter: 3 > 0, so parking is allowed
4
Update State
Medium counter decrements to 2, return true to car owner
Key Takeaway
🎯 Key Insight: Sometimes the simplest solution is the best! Instead of complex data structures, three integer counters provide instant O(1) access and updates, making this system incredibly efficient and scalable.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Each addCar operation only requires a simple counter check and decrement

n
2n
Linear Growth
Space Complexity
O(1)

Only need three integer variables regardless of parking lot size

n
2n
Linear Space

Constraints

  • 0 ≤ big, medium, small ≤ 1000
  • carType is 1, 2, or 3
  • At most 1000 calls will be made to addCar
  • Important: Cars can only park in their designated size category
Asked in
Amazon 45 Google 32 Microsoft 28 Apple 15
42.0K Views
Medium Frequency
~8 min Avg. Time
1.9K 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