Skip to main content

Design Parking System - Solution & Explanation

EasyDesignSimulationCounting19 min readAsked at: Amazon, Meta, Tesla +3
Practice this problem

Problem Statement

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
  • At most 1000 calls will be made to addCar

Approach Overview

Problem Overview: You need to design a parking system that manages three types of slots: big, medium, and small. Each call to addCar(carType) attempts to park a car of the given type and should return true if a slot is available or false otherwise.

This is a classic design and simulation problem. Instead of searching through parking spots, the key observation is that you only need to track how many spaces remain for each car type. Every operation becomes a simple counter update.

Approach 1: Use Array for Slot Management (Time: O(1), Space: O(1))

Store the number of available slots for each car type in an array of size 3. Map car types directly to indices: 1 → big, 2 → medium, 3 → small. During initialization, fill the array with the provided capacities. When addCar is called, check the corresponding index in the array. If the value is greater than zero, decrement it and return true; otherwise return false. This approach works because every operation becomes a constant-time lookup and update, which fits perfectly with counting-based problems where you only track remaining capacity rather than individual items.

Approach 2: Use Separate Variables for Slot Management (Time: O(1), Space: O(1))

Instead of an array, maintain three integer variables: big, medium, and small. The constructor initializes these variables with the provided slot counts. In the addCar method, use conditional checks on carType. If the matching variable is positive, decrement it and return true. Otherwise return false. This approach is slightly more explicit and avoids indexing logic, which some developers find easier to read during interviews.

The main difference between the two approaches is code structure. The array version is compact and scales easily if more slot types are added. The variable-based version is straightforward and often preferred for quick implementation.

Recommended for interviews: Both approaches achieve optimal O(1) time and O(1) space complexity, which is exactly what interviewers expect for this problem. The array approach demonstrates clean abstraction and extensibility, while the separate variable approach shows clarity and simplicity. Showing either solution quickly and explaining why constant-time operations are sufficient signals strong understanding of system state management.

Approach 1: Approach 1: Use Array for Slot Management

This approach leverages an array to manage the count of available parking slots for each type of car. The array indices correspond to the car types: index 0 for big cars, index 1 for medium, and index 2 for small. The addCar function decrements the corresponding index if space is available.

The code uses a struct with an array of size 3 to store slots available for each car type. carType is 1-indexed due to the problem constraints but is used as 0-indexed for array access. The parkingSystemAddCar function decrements the respective slot count if the slot is available. Memory is dynamically allocated and should be freed when done.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) for each addCar operation as we are directly accessing an array element.
Space Complexity: O(1) as only a fixed-size array is used.

Try this approach in the editor →

Approach 2: Approach 2: Use Separate Variables for Slot Management

This method uses distinct variables to handle each type of car's parking slots. This approach makes the code very clear for small data sets. While this isn't necessarily more efficient than the array method for this particular problem, it offers an alternative for simple systems where explicit clarity is beneficial.

This C implementation uses separate integer variables to track each type of parking slot. Each addCar operation checks the respective variable based on car type and decrements it if possible.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) for checking and updating.
Space Complexity: O(1) as three variables track the state.

Try this approach in the editor →

Approach 3: Simulation

We use an array cnt of length 4 to represent the number of parking spaces for each type of car, where cnt[1], cnt[2], and cnt[3] represent the number of large, medium, and small parking spaces, respectively.

During initialization, we set cnt[1], cnt[2], and cnt[3] to the number of large, medium, and small parking spaces, respectively.

Each time a car parks, we check if there is a corresponding parking space in the parking lot. If not, we return false; otherwise, we decrement the number of corresponding parking spaces by one and return true.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C#

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Use Array for Slot Management

Time Complexity: O(1) for each addCar operation as we are directly accessing an array element.
Space Complexity: O(1) as only a fixed-size array is used.

Approach 2: Use Separate Variables for Slot Management

Time Complexity: O(1) for checking and updating.
Space Complexity: O(1) as three variables track the state.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Array for Slot ManagementO(1)O(1)Clean mapping of car type to index. Easy to extend if more parking categories are introduced.
Separate Variables for Slot ManagementO(1)O(1)Simplest implementation with explicit variables. Good for interviews where readability matters.

Video Solution

Design Parking System - Leetcode 1603 - Python • NeetCodeIO • 17,274 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Design Parking System easy or hard?
Design Parking System is classified as an Easy problem on LeetCode with a high acceptance rate of over 80%. The challenge focuses on modeling a simple system and maintaining counters correctly rather than implementing complex algorithms.
How to solve Design Parking System in O(n)?
An O(n) solution is unnecessary for this problem. The optimal design tracks the number of available slots for each car type and updates the count when a car parks. With this approach, each operation runs in O(1) time because it only involves a simple counter check and decrement.
Design Parking System Python or Java solution
In Python or Java, the typical solution stores the remaining slot counts in either an array or class variables. The constructor initializes these counts, and the addCar method checks the requested type and decreases the count if space is available. Each call runs in O(1) time.
What is the best approach for Design Parking System?
The best approach is to store the remaining capacity of each parking type and update it during every addCar call. Using either an array of size three or three separate variables allows constant-time checks and updates. Both implementations achieve O(1) time and O(1) space complexity, which is optimal for this design problem.
What data structure is used in Design Parking System?
The problem typically uses simple counting with either an array or individual integer variables. An array maps car types to indices, while separate variables directly represent big, medium, and small slot counts. Both approaches rely on constant-time updates.
What is the time complexity of Design Parking System?
The time complexity is O(1) per operation. Each addCar call performs a direct lookup of the remaining slot count and optionally decrements it. No iteration or search is required, so the operation always runs in constant time.
Is Design Parking System asked at Google, Amazon, or Meta?
Design-style problems like this frequently appear in interviews at large tech companies including Amazon and Google. They test whether you can model a system, maintain state correctly, and implement efficient constant-time operations.

Ready to solve this problem?

Practice Design Parking System with our built-in code editor and test cases.

Practice on FleetCode