Design Parking System

Updated on 22 May, 2025
Design Parking System header image

Problem Statement

This system requires designing a class called ParkingSystem to manage a parking lot that segregates parking spaces into three categories: big, medium, and small. Each category is allocated a fixed number of parking slots. The class should support:

  1. Initialization with a specified number of parking slots for each type of parking space.
  2. The ability to park a car in a correctly sized slot based on its type. The car types are categorized and represented by integers: 1 for big, 2 for medium, and 3 for small. The method should check if there is an available slot for the car's type, park the car if possible, and return whether the parking attempt was successful.

For instance, upon attempting to park a car in a full slot or an incorrect slot type, the system should return false. If the slot is correct and available, the method should return true.

Examples

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 and Intuition

Understanding how to tackle the design of the ParkingSystem class involves interpreting the provided examples and respecting the outlined constraints.

  • Initialization: The ParkingSystem class constructor accepts three arguments: big, medium, and small. These indicate the total available slots for each respective car type at the initiation. This approach suggests we need to maintain a state within the class to track the number of slots used up or available for each type over the object's lifecycle.

  • Parking a Car:

    • The core method involved here is addCar(int carType), which attempts to park a car into its respective slot based on the given carType.
    • It needs to:
      • Verify if a slot is available by checking if the count for that specific car type has not reached its maximum.
      • If available, it allows the car to park by decrementing an available space, returning true.
      • If not available, it refuses parking by returning false.
  • Handling Constraints:

    • Given the limitation of at most 1000 callstoaddCarand the range forbig, medium, and smallbeing from0to1000`, it's feasible to maintain this state directly via instance variables inside the class with continuous decrementing and checking operations, which are O(1) in time complexity.

From the example provided, for instance, initializing a parking system where slots for big, medium, and small cars are set as 1, 1, 0 respectively, then through subsequent calls to addCar(), we manage the state transitions based on car arrivals:

  • Adding a big car (carType = 1) successfully updates the slot count for big cars from 1 to 0 and returns true.
  • Adding another big car now returns false due to no available slots (state is maintained from earlier operations).

This practical application demonstrates how state management within the class allows the parking system to function correctly based on the initial configurations and the real-time updates from parking actions.

Solutions

  • C++
  • Java
  • C
  • JavaScript
  • Python
cpp
class CarParkingSystem {
public:
    vector<int> slots;

    CarParkingSystem(int big, int medium, int small) {
        slots = vector<int>{big, medium, small};
    }

    bool parkCar(int type) {
        if (slots[type - 1] > 0) {
            slots[type - 1]--;
            return true;
        }
        return false;
    }
};

Design a car parking system using C++. The solution involves creating a CarParkingSystem class that manages three types of parking slots: big, medium, and small. Follow these steps to handle the parking logic:

  1. Initialize the class with three parameters, big, medium, and small, to represent the number of slots for each car type.

  2. Use a vector slots to dynamically store the counts of each type of parking slot.

  3. Implement the parkCar method:

    • The method takes one parameter, type, denoting the car type (1 for big, 2 for medium, 3 for small).
    • Verify if there are available slots for the given type by checking if the corresponding value in the slots vector is greater than zero.
    • If a slot is available, decrement the slot count for that type and return true to indicate successful parking.
    • If no slots are available, return false.

This implementation effectively tracks the number of available parking spots for each car type and updates the availability dynamically as cars are parked.

java
class VehicleParkingSystem {

    int[] slotsAvailable;

    public VehicleParkingSystem(int big, int medium, int small) {
        this.slotsAvailable = new int[]{big, medium, small};
    }

    public boolean parkCar(int type) {
        if (this.slotsAvailable[type - 1] > 0) {
            this.slotsAvailable[type - 1]--;
            return true;
        }
        return false;
    }
}

This solution outlines a parking system for vehicles through the VehicleParkingSystem class implemented in Java. You manage three types of parking slots: big, medium, and small.

  • Initialize VehicleParkingSystem by specifying the number of slots available for each type of vehicle:
java
int[] slotsAvailable;

public VehicleParkingSystem(int big, int medium, int small) {
    this.slotsAvailable = new int[]{big, medium, small};
}
  • Attempt to park a car using the parkCar method, which takes an integer representing the type of car (1 for large cars, 2 for medium-sized cars, and 3 for small cars):
java
public boolean parkCar(int type) {
    if (this.slotsAvailable[type - 1] > 0) {
        this.slotsAvailable[type - 1]--;
        return true;
    }
    return false;
}

In the VehicleParkingSystem method, instantiate the array with the counts of available slots for each type. The parkCar method checks if the slot count for that specific type of vehicle is greater than zero. If so, it decrements the count and returns true to indicate the car was successfully parked. If not, it returns false, indicating the parking attempt was unsuccessful.

c
typedef struct {
    int openSlots[3]; // Slots for types of vehicles
} CarPark;

CarPark* createCarPark(int large, int medium, int compact) {
    CarPark* system = (CarPark*) malloc(sizeof(CarPark));
    system->openSlots[0] = large;
    system->openSlots[1] = medium;
    system->openSlots[2] = compact;
    return system;
}

bool addVehicleToPark(CarPark* system, int vehicleType) {
    if (system->openSlots[vehicleType - 1] > 0) {
        system->openSlots[vehicleType - 1]--;
        return true;
    }
    return false;
}

void destroyCarPark(CarPark* system) {
    free(system);
}

The C programming solution provided outlines a basic system to manage a parking lot with three types of parking spaces: large, medium, and compact. The primary components of this solution include structures and functions to handle the parking system's operations.

  • Structure Definition: A CarPark struct is defined with an array openSlots to keep track of available slots for each vehicle type.

  • Creating a Car Park: The function createCarPark(int large, int medium, int compact) initializes a CarPark with the number of available slots for each vehicle type as inputs. Memory allocation is handled using malloc, and the initial slot counts are set based on the provided arguments.

  • Adding a Vehicle: The function addVehicleToPark(CarPark* system, int vehicleType) checks if there is available space for the specified type of vehicle (large, medium, compact). Vehicle types are input as integers (1 for large, 2 for medium, 3 for compact). If space is available, it decrements the count of open slots for that vehicle type and returns true. If no spaces are available, it returns false.

  • Destroying a Car Park: To clean up resources, destroyCarPark(CarPark* system) is used to free the allocated memory for the parking system.

Use this implementation for straightforward parking management in applications that need to handle various vehicle sizes without dependencies on external libraries or complex data structures.

js
var CarPark = function(large, medium, compact) {
    this.slots = [large, medium, compact];
};

CarPark.prototype.parkCar = function(type) {
    if (this.slots[type - 1] > 0) {
        this.slots[type - 1]--;
        return true;
    }
    return false;
};

Create a simple car parking system using JavaScript to manage parking slots for large, medium, and compact cars. The CarPark function initializes the parking lot with a preset number of slots for each type of car. The parkCar method is then used to park a car based on its type, decrementing the available slots accordingly and returning a success status.

  • Define the CarPark constructor that takes three parameters: large, medium, and compact. These parameters represent the number of available slots for each car type.

    javascript
    var CarPark = function(large, medium, compact) {
        this.slots = [large, medium, compact];
    };
    
  • Add the parkCar method to the CarPark prototype. This method takes the type of car (1 for large, 2 for medium, 3 for compact) as a parameter.

    javascript
    CarPark.prototype.parkCar = function(type) {
        if (this.slots[type - 1] > 0) {
            this.slots[type - 1]--;
            return true;
        }
        return false;
    };
    

This implementation efficiently tracks the number of available parking slots for each type of car and indicates whether a car can be parked in the respective slot. This system is particularly useful for parking management in locations like malls and offices, ensuring an organized distribution of parking spaces according to car size.

python
class ParkingManager:

    def __init__(self, large: int, medium: int, compact: int):
        self.slots = [large, medium, compact]

    def parkCar(self, type: int) -> bool:
        if self.slots[type - 1] > 0:
            self.slots[type - 1] -= 1
            return True
        return False

The solution implements a ParkingManager class in Python to manage a parking system for three types of parking spots: large, medium, and compact. The class provides two main functionalities:

  1. Initialize parking capacities:

    • During the initialization (__init__), it takes three integers representing the number of slots available for large, medium, and compact cars respectively. These values are stored in a list named slots.
  2. Parking a car:

    • The method parkCar accepts an integer (type) as input, which corresponds to the car type (1 for large, 2 for medium, and 3 for compact).
    • It checks if there is an available slot for the specified car type. If available, it decrements the count of that slot type by one and returns True, indicating the car was successfully parked.
    • If no slots are available for the given type, the method returns False.

This setup provides a straightforward method to manage parking within a system with specified capacities for different types of cars. The class can easily be integrated into a larger system where car parking operations are needed.

Comments

No comments yet.