Track Capacity in Three Parking Categories

Quick Overview

Track Capacity in Three Parking Categories. Implement ParkingSystem with three independent capacities: big, medium, and small. addCar(carType) receives 1, 2, or 3 for those categories.

Track Capacity in Three Parking Categories

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Implement `ParkingSystem` with three independent capacities: `big`, `medium`, and `small`. `addCar(carType)` receives 1, 2, or 3 for those categories. A car may use only its own category. Park it and return true when a slot remains; otherwise return false without changing the state. ```cpp class ParkingSystem { public: ParkingSystem(int big, int medium, int small); bool addCar(int carType); }; ``` ### Examples **Example 1:** ```text ParkingSystem p(0,2,1); p.addCar(1) -> false p.addCar(2) -> true p.addCar(2) -> true p.addCar(2) -> false p.addCar(3) -> true ``` **Example 2:** ```text ParkingSystem p(2,0,0); p.addCar(3) -> false p.addCar(1) -> true p.addCar(1) -> true p.addCar(1) -> false Explanation: Small cars cannot borrow a big slot. ``` ### Constraints - Each initial capacity is an integer in `[0,1000]`. - `carType` is one of `1`, `2`, and `3`. - There are at most `1000` calls to addCar. ### Clarifying Questions Capacity persists across calls, and there is no departure operation. A larger parking category cannot substitute for a requested smaller category. ```hint Inspect the structure What state changes are allowed after a successful parking request, and after a rejected one? ``` ### Reasoning checkpoints - Map the three type codes to their own capacities. - Keep rejected calls from consuming any slot. ### Optional extensions What extra state would a removeCar operation require to distinguish empty from occupied slots?

Overview: Track Capacity in Three Parking Categories. Implement ParkingSystem with three independent capacities: big, medium, and small. addCar(carType) receives 1, 2, or 3 for those categories.

Microsoft logo
Microsoft
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement ParkingSystem with three independent capacities: big, medium, and small. addCar(carType) receives 1, 2, or 3 for those categories. A car may use only its own category. Park it and return true when a slot remains; otherwise return false without changing the state.

class ParkingSystem {
public:
    ParkingSystem(int big, int medium, int small);
    bool addCar(int carType);
};

Examples

Example 1:

ParkingSystem p(0,2,1);
p.addCar(1) -> false
p.addCar(2) -> true
p.addCar(2) -> true
p.addCar(2) -> false
p.addCar(3) -> true

Example 2:

ParkingSystem p(2,0,0);
p.addCar(3) -> false
p.addCar(1) -> true
p.addCar(1) -> true
p.addCar(1) -> false
Explanation: Small cars cannot borrow a big slot.

Constraints

  • Each initial capacity is an integer in [0,1000] .
  • carType is one of 1 , 2 , and 3 .
  • There are at most 1000 calls to addCar.

Clarifying Questions Guidance

Capacity persists across calls, and there is no departure operation. A larger parking category cannot substitute for a requested smaller category.

Reasoning checkpoints

  • Map the three type codes to their own capacities.
  • Keep rejected calls from consuming any slot.

Optional extensions

What extra state would a removeCar operation require to distinguish empty from occupied slots?