Course content
Design a Parking Lot
Medium·Tagsllddesignparking-lotencapsulationpolymorphism
Problem Statement
Design a parking lot with three spot sizes — `SMALL`, `MEDIUM`, `LARGE` — and three vehicle types — `BIKE`, `CAR`, `TRUCK`. Smaller vehicles can park in larger spots, but not the other way around. The lot must allocate the smallest spot that fits to maximise utilisation: a bike with small spots available goes in a small spot even if medium and large spots are also free.
Compatibility rules
- `BIKE` fits in `SMALL`, `MEDIUM`, or `LARGE`.
- `CAR` fits in `MEDIUM` or `LARGE`.
- `TRUCK` fits in `LARGE` only.
API contract
```java
enum VehicleType { BIKE, CAR, TRUCK }
enum SpotSize { SMALL, MEDIUM, LARGE }
class Vehicle {
public Vehicle(VehicleType type);
public VehicleType getType();
}
class ParkingLot {
public ParkingLot(int small, int medium, int large);
/** Parks the vehicle in the smallest fitting spot.
* Returns a non-null ticket ID on success, or null if no spot fits. */
public String park(Vehicle v);
/** Frees the spot held by ticketId. Returns true if the ticket was valid,
* false if the ticket is unknown (already left, or never issued). */
public boolean leave(String ticketId);
/* Returns the number of currently available spots of the given size. /
public int availableSpots(SpotSize size);
}
```
The validator runs three checks:
1. smallest_fitting_spot — a bike with all three sizes available is placed in `SMALL`; a bike with only `MEDIUM` and `LARGE` available is placed in `MEDIUM`; a car with `MEDIUM` and `LARGE` available is placed in `MEDIUM`; a truck in any lot with `LARGE` available is placed in `LARGE`. After each `park`, `availableSpots(...)` reflects exactly which size shrank.
2. oversized_or_full_returns_null — a truck in a lot with no `LARGE` spots returns `null` from `park`. A bike in a fully-occupied lot also returns `null`. The lot does not throw.
3. leave_frees_spot — after `park` then `leave(ticket)`, `availableSpots` is restored and a fresh `park` of the same vehicle reuses the same size. `leave` of an invalid ticket returns `false` and changes nothing.
The stub `ParkingLot` throws `UnsupportedOperationException` from every method, so it fails every check until you implement it.
Starter code and validators are available in Java, Python, and C++.
Examples
Example 1
Input
var lot = new ParkingLot(1, 1, 1); lot.park(new Vehicle(VehicleType.BIKE)); lot.availableSpots(SpotSize.SMALL)Output
"0"Why
Bike fits in SMALL, MEDIUM, or LARGE. The smallest-fitting rule picks SMALL; SMALL count drops from 1 to 0.
Example 2
Input
var lot = new ParkingLot(0, 1, 1); lot.park(new Vehicle(VehicleType.CAR)); lot.availableSpots(SpotSize.MEDIUM)Output
"0"Why
Car cannot fit in SMALL. Smallest fitting size is MEDIUM.
Example 3
Input
var lot = new ParkingLot(2, 0, 0); lot.park(new Vehicle(VehicleType.TRUCK))Output
"null"Why
Truck only fits in LARGE; the lot has zero LARGE spots. park returns null instead of throwing.
Constraints
- •ParkingLot has a public constructor with three int parameters: small, medium, large counts.
- •park(Vehicle) returns a non-null String ticket on success, or null if no compatible spot is available.
- •park must place the vehicle in the smallest spot size that fits its type. SMALL < MEDIUM < LARGE.
- •leave(ticket) returns true if the ticket was issued (and not already returned), false otherwise.
- •availableSpots(SpotSize) returns the count of currently free spots of that size.
- •park must not throw for an oversized vehicle or a full lot — it returns null.
Hints
Stuck? Reveal a nudge toward the right pattern, one step at a time.
Hint 1
Three int counters (smallFree, mediumFree, largeFree) plus a Map<String, SpotSize> from ticket → spot size is enough. No need to model individual ParkingSpot objects unless the design specifically asks for it.
Hint 2
park(v): write a helper canFitIn(VehicleType, SpotSize) returning true/false. Try sizes in order SMALL → MEDIUM → LARGE; the first one that fits AND has free capacity wins.
Hint 3
Generate ticket IDs by an int counter incremented on each successful park ('T-1', 'T-2', ...). They just need to be unique non-null strings.
Hint 4
leave(ticket): look up the size, increment that counter, remove from the map. Return true. If the ticket isn't in the map, return false — do not throw.
Hint 5
park must return null (not throw) when no spot fits. Use a clear no-fit signal so callers can branch.