Design Parking System

Design a parking lot with three kinds of spaces: big, medium and small, with a fixed number of each. ParkingSystem(big, medium, small) sets the counts. addCar(carType) tries to park a car of type 1 (big), 2 (medium) or 3 (small); a car may only use a space of its own size. Return true and take the space if one is free, otherwise return false.

30:00

Design Parking System
easy
Free written solution
Topics
Companies

Design a parking lot with three kinds of spaces: big, medium and small, with a fixed number of each. ParkingSystem(big, medium, small) sets the counts. addCar(carType) tries to park a car of type 1 (big), 2 (medium) or 3 (small); a car may only use a space of its own size. Return true and take the space if one is free, otherwise return false.

Example 1:
Input: {"commands":["ParkingSystem","addCar","addCar","addCar","addCar"],"values":[[1,1,0],[1],[2],[3],[1]]}
Output: [null,true,true,false,false]
Constraints:
  • 0≤big,medium,small≤10000 \leq \text{big}, \text{medium}, \text{small} \leq 1000

  • carType is 1, 2, or 3

  • At most 1000 calls will be made to addCar.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["ParkingSystem","addCar","addCar","addCar","addCar"],"values":[[1,1,0],[1],[2],[3],[1]]}
new(1, 1, 0)
addCar(1)
addCar(2)
addCar(3)
addCar(1)

One counter per space size is all the state needed: index 1 = big, 2 = medium, 3 = small, so carType indexes it directly.

gate

no car

slots = [0, 0, 0, 0] (free spaces per size)

parked 0 rejected 0

1: big

slots[1] = 0 / 0

no big spaces in this lot

2: medium

slots[2] = 0 / 0

no medium spaces in this lot

3: small

slots[3] = 0 / 0

no small spaces in this lot

Returned values

[]
Variables
VariableValue
slots[0, 0, 0]
DepthFunction Call
Stack empty
0/10