Parking Lot
Problem
Design ParkingLot to manage a fixed inventory of compact, regular, and large parking spots, assign each vehicle the best available compatible spot, and release spots when vehicles leave.
Requirements
- Fixed inventory. Construction creates an initially empty inventory whose capacities never change.
- Generated identity. Compact spots are named
C-1,C-2, and so on; regular spots useR-; large spots useL-. Each suffix starts at 1 and is unique within its category. - Vehicle compatibility. A motorcycle may use a compact, regular, or large spot. A car may use a regular or large spot. A truck may use only a large spot.
- Category priority. A motorcycle tries compact, then regular, then large. A car tries regular, then large. A truck tries large.
- Lowest spot. Allocation chooses the first compatible category with free capacity, then the free spot with the lowest numeric suffix in that category.
- Successful parking. A successful
parkcall occupies exactly one spot, records exactly one vehicle assignment, and returns that spot's ID. - Duplicate rejection. If the vehicle is already parked,
parkreturns""without changing its assignment or any availability count. - Full rejection. If no compatible spot is free,
parkreturns""without changing the lot. - Successful departure.
unparkreleases the vehicle's assigned spot, removes its assignment, and returnstrue. - Missing departure.
unparkreturnsfalsewithout changing the lot when the vehicle is not parked. - Location query.
locatereturns the vehicle's assigned spot ID, or""when the vehicle is not parked, without changing the lot. - Availability query.
availableSpotsreturns the number of free spots in exactly the requested category without changing the lot. - Reentry. A vehicle ID may park again with any valid vehicle type after a successful
unpark. - Invalid input. Every argument is validated before existing state is inspected. An invalid argument raises
ValueErrorin Python,IllegalArgumentExceptionin Java, orstd::invalid_argumentin C++, and leaves the lot unchanged. - Sequential execution. Calls execute one at a time in invocation order, return immediately, and never wait for future capacity.
API
| Signature | Returns | Behavior |
|---|---|---|
ParkingLot(compactSpots: integer, regularSpots: integer, largeSpots: integer) | Not applicable | Creates the fixed empty inventory. Each capacity must be from 0 through 10,000, and their sum must be positive. Invalid capacities raise the language-specific invalid-argument exception. |
park(vehicleId: string, vehicleType: string) | string | Validates both arguments, then parks the absent vehicle in the selected compatible spot and returns its ID. Returns "" without mutation for a duplicate vehicle or when no compatible spot is free. |
unpark(vehicleId: string) | boolean | Validates the ID. Releases its spot and returns true, or returns false without mutation when the vehicle is absent. |
locate(vehicleId: string) | string | Validates the ID. Returns its assigned spot ID, or "" when absent, without mutation. |
availableSpots(spotType: string) | integer | Accepts COMPACT, REGULAR, or LARGE and returns that category's free count without mutation. Any other value raises the language-specific invalid-argument exception. |
vehicleType accepts only MOTORCYCLE, CAR, or TRUCK. A valid vehicleId contains 1 through 32 ASCII characters. Its first character is a letter or digit. Each remaining character is a letter, digit, hyphen, or underscore. All names are case-sensitive.
Examples
For ParkingLot(2, 1, 1):
| Step | Operation | Result |
|---|---|---|
| 1 | park("m1", "MOTORCYCLE") | "C-1" |
| 2 | park("m2", "MOTORCYCLE") | "C-2" |
| 3 | park("car1", "CAR") | "R-1" |
| 4 | park("car2", "CAR") | "L-1" |
| 5 | park("truck1", "TRUCK") | "" |
| 6 | locate("car1") | "R-1" |
| 7 | availableSpots("LARGE") | 0 |
| 8 | unpark("car1") | true |
| 9 | park("m3", "MOTORCYCLE") | "R-1" |
Constraints
- each category capacity .
- Total capacity is from 1 through 30,000.
- A testcase contains at most 50,000 method calls and 50,000 distinct vehicle IDs.
- Availability counts fit in a signed 32-bit integer and never become negative or exceed their constructed capacities.
- The implementation performs no filesystem, network, clock, or random-number input or output.
Notes
Concurrency, waiting, timeouts, reservations, billing, floors, dynamic capacity, persistence, and external services are outside this problem's scope.