Design Parking System

EasyDesigndesign

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A parking lot has three kinds of slots: big, medium and small, with a fixed number of each. Implement ParkingSystem: - ParkingSystem(big, medium, small) creates a lot with that many slots of each size. - addCar(carType) tries to park a car of type 1 (big), 2 (medium) or 3 (small). A car may only use a slot of its own size. If a free slot of that size exists, the car takes it and the method returns true; otherwise it returns false and nothing changes. Cars never leave once parked. Each call should run in O(1).

Examples

Input: ["ParkingSystem","addCar","addCar","addCar","addCar","addCar","addCar"] [[2,0,1],[1],[2],[3],[1],[1],[3]]
Output: [null,true,false,true,true,false,false]
There are 2 big slots, 0 medium and 1 small. Two big cars fit, the third does not; the medium car never fits; one small car fits and the second is turned away.
Input: ["ParkingSystem","addCar","addCar","addCar"] [[0,0,0],[3],[2],[1]]
Output: [null,false,false,false]
A lot with no slots rejects every car.

Constraints

  • 0 <= big, medium, small <= 1000
  • `carType` is 1, 2 or 3.
  • At most 1000 calls are made to `addCar`.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More design problems