Problemparking lot

Session: Sign in to solve

Solution.txt

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 use R-; large spots use L-. 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 park call occupies exactly one spot, records exactly one vehicle assignment, and returns that spot's ID.
  • Duplicate rejection. If the vehicle is already parked, park returns "" without changing its assignment or any availability count.
  • Full rejection. If no compatible spot is free, park returns "" without changing the lot.
  • Successful departure. unpark releases the vehicle's assigned spot, removes its assignment, and returns true.
  • Missing departure. unpark returns false without changing the lot when the vehicle is not parked.
  • Location query. locate returns the vehicle's assigned spot ID, or "" when the vehicle is not parked, without changing the lot.
  • Availability query. availableSpots returns 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 ValueError in Python, IllegalArgumentException in Java, or std::invalid_argument in 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

SignatureReturnsBehavior
ParkingLot(compactSpots: integer, regularSpots: integer, largeSpots: integer)Not applicableCreates 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)stringValidates 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)booleanValidates the ID. Releases its spot and returns true, or returns false without mutation when the vehicle is absent.
locate(vehicleId: string)stringValidates the ID. Returns its assigned spot ID, or "" when absent, without mutation.
availableSpots(spotType: string)integerAccepts 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):

StepOperationResult
1park("m1", "MOTORCYCLE")"C-1"
2park("m2", "MOTORCYCLE")"C-2"
3park("car1", "CAR")"R-1"
4park("car2", "CAR")"L-1"
5park("truck1", "TRUCK")""
6locate("car1")"R-1"
7availableSpots("LARGE")0
8unpark("car1")true
9park("m3", "MOTORCYCLE")"R-1"

Constraints

  • 00 \leq each category capacity 10,000\leq 10{,}000.
  • 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.