Parking Lot
Mediumconcurrencystate machinestrategyidempotency
Design the software for a multi-floor parking lot that several entry gates use at the same time.
Requirements
- Spots and vehicles. Spots are
SMALL,MEDIUMorLARGE. ABIKEfits any spot, aCARfitsMEDIUMorLARGE, and aTRUCKfits onlyLARGE. You choose which fitting spot to assign, but explain your allocation rule in comments. - Park.
park(plate, type, idempotencyKey)assigns a free spot and returns a ticket id.- Plates are case-insensitive and trimmed (
" ka01 "is the same car as"KA01"). - A plate that already has an active ticket is rejected with
DuplicateVehicleException. - If no fitting spot is free, it throws
ParkingFullException. - Retrying with the same idempotency key returns the same ticket id and does not take a second spot.
- Plates are case-insensitive and trimmed (
- Pay.
pay(ticketId)returns the fee in paise: the rate for the vehicle type ร the number of started hours, with a minimum of 1 hour. For example, 61 minutes is 2 hours and exactly 60 minutes is 1 hour. Read time only from theClockgiven toSolution.create. Paying again returns the first fee and doesn't charge again. - Exit.
exit(ticketId)frees the spot. A ticket movesACTIVE -> PAID -> EXITED:- exit before paying ->
InvalidStateException - exit twice ->
InvalidStateException - pay after exit ->
InvalidStateException - unknown ticket ->
NotFoundException
- exit before paying ->
- Queries.
spotOf(ticketId)returns the spot id held by an active ticket.freeSpots()returns the free count for every size. - Validation. A blank plate or key is rejected with
IllegalArgumentException.Solution.createrejects an invalid config (no floors, negative counts, a floor with zero spots, missing or negative rates) withIllegalArgumentException.
Non-functional requirements
- Never allocate a spot twice, even when many gates call
parkat the same moment. - A car can never hold two active tickets, even when it shows up at two gates at once.
- Gates must not block each other on unrelated spots, so avoid one global lock around everything.
- Allocation and pricing should be easy to extend (new vehicle types, weekend pricing, nearest-first allocation).
What you get
api/holds the contract the hidden tests use. It's read-only.Solution.create(config, clock)is where you build and return your implementation.Mainis your demo. The judge runs it, so show the happy path, a few rejected cases and a concurrent burst.
Add as many classes and packages as you need. The judge scores correctness (hidden tests, including multi-threaded ones) and design quality (SOLID, extensibility, thread safety, input validation). Each counts for 50%.