Start learning free

LESSON 635 OF 639 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Parking Lot in Scala

Practice O(log n)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build ParkingLot. It gives cars a spot and frees the spot when they leave.

The lot has small, medium and large spots. Each size numbers its spots from 1. A spot's name is its size letter and its number, such as S3, M1 or L2.

Implement the class:

  • ParkingLot(small, medium, large) makes a lot with that many spots of each size. All of them start free.
  • park(plate, size) parks the car with this plate. It returns the name of the spot the car gets.
  • leave(plate) frees the car's spot. It returns true if the car was parked, and false if not.
  • free(size) returns how many spots of this size are free.

A car gets the smallest size of spot that fits it. A small car fits any spot. A medium car fits a medium or a large spot. A large car fits only a large spot.

Within a size, the car takes the free spot with the lowest number.

If no spot fits, park returns "full". If the plate is already parked, park returns "already parked". Then nothing changes.

The calls run in order on one new ParkingLot. The output has one line for each call, with what it returns. Booleans are written as True and False.

 

Example 1:

Input:
ParkingLot(1, 1, 1)
1. park("AB12", "small")
2. park("CD34", "small")
3. park("EF56", "small")
4. park("GH78", "small")
5. free("large")
6. leave("CD34")
7. park("GH78", "medium")
8. park("AB12", "large")
Output:
S1
M1
L1
full
0
True
M1
already parked
Explanation:
Call 1 takes the only small spot.
Calls 2 and 3 find no small spot free.
So they take the medium spot, then the large one.
Call 4 finds every spot taken.
Call 6 frees M1, and call 7 gets it.
Call 8 changes nothing. AB12 is still in S1.

Example 2:

Input:
ParkingLot(3, 0, 2)
1. park("A1", "small")
2. park("A2", "small")
3. park("A3", "small")
4. leave("A2")
5. leave("A1")
6. park("B1", "small")
7. free("small")
8. park("B2", "medium")
9. park("B3", "large")
10. park("B4", "large")
11. leave("Z9")
Output:
S1
S2
S3
True
True
S1
1
L1
L2
full
False
Explanation:
Calls 4 and 5 free S2 and S1.
Call 6 takes S1, the lower of the two.
Only S2 is still free, so call 7 returns 1.
The lot has no medium spots, so call 8 takes L1.
Call 10 finds both large spots taken.
Z9 was never parked, so call 11 returns False.

Example 3:

Input:
ParkingLot(2, 1, 0)
1. park("KX10", "medium")
2. park("KX11", "medium")
3. free("small")
4. leave("KX10")
5. leave("KX10")
6. park("KX11", "medium")
7. park("KX10", "small")
Output:
M1
full
2
True
False
M1
S1
Explanation:
Call 2 returns full, though two small spots are free.
A medium car doesn't fit a small spot.
Call 5 returns False, because KX10 has already left.
Call 7 parks KX10 again, in a small spot this time.

 

Constraints:

  • Each spot count is a whole number from 0 to 50,000.
  • size is "small", "medium" or "large".
  • A plate has 1 to 8 characters.
  • Each character of a plate is an uppercase letter or a digit.
  • At most 100,000 calls follow the constructor.

Note:

Each call should run in O(log n) time, where n is the number of spots. Looking through every spot on each call would take O(n) time.

STEP 1 OF 22
    Ctrl + Enter to check

    Parking Lot in Scala

    Keep going with Scala500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free