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

Edge cases to test

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 True

C++

#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