Algo-Sim › Low-Level Design
Design Parking System
Easy · Low-Level Design · step through it in Java, Python and C++ on your own input
A parking lot with big, medium and small slots: the class keeps one free counter per size, and each car checks only its own pool.
Problem
A parking lot has a fixed number of big, medium and small slots. addCar(carType) parks a car of type 1 (big), 2 (medium) or 3 (small) in a free slot of exactly its size and returns true, or returns false when that size is full.
Constraints
- 0 ≤ big, medium, small ≤ 1000
- carType is 1, 2 or 3
- At most 1000 calls; each is O(1)
Edge cases to test
- A size with zero slots
- Filling a size exactly to capacity
- A car type that does not exist
Follow-up questions
- How would you extend it to a real lot?
- Model floors, spots and tickets as classes, let larger spots fit smaller cars, and track entry time for billing.
- How do you handle concurrent arrivals?
- An atomic decrement per size (or a lock per size) so two cars never take the last slot.
Solution
Java
class ParkingSystem {
private final int[] free; // free[0] big, free[1] medium, free[2] small
public ParkingSystem(int big, int medium, int small) {
free = new int[] {big, medium, small};
}
public boolean addCar(int carType) {
if (free[carType - 1] == 0) return false;
free[carType - 1]--;
return true;
}
}Python
class ParkingSystem:
def __init__(self, big, medium, small):
self.free = [big, medium, small]
def addCar(self, carType):
if self.free[carType - 1] == 0:
return False
self.free[carType - 1] -= 1
return TrueC++
#include <bits/stdc++.h>
using namespace std;
class ParkingSystem {
public:
vector<int> free; // free[0] big, free[1] medium, free[2] small
ParkingSystem(int big, int medium, int small) {
free = vector<int>{big, medium, small};
}
bool addCar(int carType) {
if (free[carType - 1] == 0)
return false;
free[carType - 1]--;
return true;
}
};
More Low-Level Design problems
- LRU Cache (Medium)
- LFU Cache (Hard)
- Min Stack (Medium)
- Insert Delete GetRandom O(1) (Medium)
- Design Circular Queue (Medium)
- Flatten Nested List Iterator (Medium)
- Rate Limiter: Token Bucket (Medium)
- Rate Limiter: Sliding Window Log (Medium)
- Logger Rate Limiter (Easy)
- Design Hit Counter (Medium)
- Design Tic-Tac-Toe (Medium)
- Design Twitter (Medium)
- Design Snake Game (Medium)
- Vending Machine (State pattern) (Medium)
- Elevator System (LOOK scheduling) (Medium)
- Splitwise (expense sharing) (Medium)
- Movie Ticket Booking (BookMyShow) (Medium)