Implement a multi-level parking lot with size-matched spots and availability counts
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a parking lot system that supports the following:
- Multiple parking levels per parking lot.
- On each level, 20% of the spots are `Small`, 50% are `Medium` and 30% are `Large`.
- Vehicles of type `Motorcycle`, `Car` and `Bus`.
- Spots of size `Small`, `Medium` and `Large`.
- Motorcycles are only allowed to park in `Small` spots.
- Cars are only allowed to park in `Medium` spots.
- Buses are only allowed to park in `Large` spots.
- Track each parked vehicle by its license plate, mapping it directly to a spot.
- Return the total number of available spots that can fit a given vehicle type.
Implement the following methods:
- `Vehicle(licensePlate: string, vehicleType: VehicleType)`: initialize a vehicle.
- `ParkingSpot(spotId: string, size: SpotSize)`: initialize a parking spot.
- `ParkingLevel(levelId: string, numSpots: number)`: initialize a parking level.
- `ParkingLot(numLevels: number, spotsPerLevel: number)`: initialize a parking lot.
- `parkVehicle(vehicle: Vehicle) -> boolean`: if the vehicle is already parked or there are no available spaces, return `false`. Otherwise park the vehicle and return `true`.
- `removeVehicle(licensePlate: string) -> boolean`: if the vehicle is not in the parking lot, return `false`. Otherwise remove the vehicle and return `true`.
- `getAvailableSpots(vehicleType: VehicleType) -> number`: given a vehicle type, return the number of available spots in the parking lot for that type of vehicle.
For example, a lot with one level of 10 spots has 2 `Small`, 5 `Medium` and 3 `Large` spots:
```text
ParkingLot(1, 10)
parkVehicle(Vehicle("CAR-1", Car)) -> true
parkVehicle(Vehicle("CAR-1", Car)) -> false (already parked)
getAvailableSpots(Car) -> 4
parkVehicle(Vehicle("MOTO-1", Motorcycle)) -> true
parkVehicle(Vehicle("MOTO-2", Motorcycle)) -> true
parkVehicle(Vehicle("MOTO-3", Motorcycle)) -> false (both Small spots are taken)
getAvailableSpots(Motorcycle) -> 0
removeVehicle("MOTO-1") -> true
removeVehicle("MOTO-1") -> false (no longer parked)
getAvailableSpots(Motorcycle) -> 1
getAvailableSpots(Bus) -> 3
```
```hint Keep the counts cheap
Scanning every spot to answer `getAvailableSpots` or to find a free spot is correct, but it gets slow when both the call count and the spot count approach 10,000. Ask what state you could update on each park and remove so those answers become lookups.
```
### Constraints & Assumptions
- The number of vehicles and the number of parking levels are each between 1 and 100.
- The number of parking spots is between 10 and 10,000.
- At most 10,000 calls in total are made to `parkVehicle`, `removeVehicle` and `getAvailableSpots`.
- When a level's spot count is not a multiple of 10, round the `Small` and `Medium` counts down and give the remainder to `Large`, so the three counts always add up to the level size.
- A vehicle never parks in a spot of another size, even when every spot of its own size is taken.
### Clarifying Questions to Ask
- Does it matter which free spot a vehicle gets, for example filling lower levels first?
- If a license plate is already parked and arrives again with a different vehicle type, should that still count as already parked?
- Will several entry gates call `parkVehicle` at the same time, or can I assume single-threaded access?
### What a Strong Answer Covers
- A class model with vehicle type and spot size enums, and a single place that maps each vehicle type to the one spot size it may use.
- Level setup that applies the 20/50/30 split with a stated rounding rule, so the counts always sum to the level size.
- A license plate to spot map that rejects double parking and turns removal into a direct lookup.
- State that keeps `parkVehicle` and `getAvailableSpots` fast, with time and space complexity for each operation.
- Edge cases such as one size being full while others have room, removing an unknown plate, and reusing a spot after removal.
### Follow-up Questions
- How would the model change if a bus needed five adjacent `Large` spots instead of one?
- How would you let a car use a `Large` spot when every `Medium` spot is taken, and what happens to `getAvailableSpots` then?
- How would you keep `parkVehicle` correct when several entry gates call it concurrently?
- How would you add parking tickets and hourly pricing without rewriting the spot allocation?
Overview: Implement an object-oriented parking lot with several levels, three spot sizes and three vehicle types, where each vehicle fits exactly one spot size. It tests class design, a license plate lookup for parking and removal, and keeping availability counts fast under thousands of calls.