Movie Ticket Booking
Hardconcurrencydeadlockstate machineexpiry
Design the seat-booking core of a movie ticketing app. When a popular show opens, thousands of users try to grab the same seats.
Requirements
- Hold.
hold(showId, seatIds, userId)reserves all the requested seats or none of them, and returns a hold id.- If any seat is booked, or held by a hold that hasn't expired, it throws
SeatUnavailableException. After a failure, nothing is held. - An unknown show or seat throws
NotFoundException. - A null or empty seat list, a seat repeated in one request, or a blank user id throws
IllegalArgumentException.
- If any seat is booked, or held by a hold that hasn't expired, it throws
- Expiry. A hold blocks its seats for
holdTtl. It is expired oncenow >= createdAt + holdTtl, and its seats are free again without any background job. Read time only from theClockpassed toSolution.create. - Confirm.
confirm(holdId)turns the hold into a booking, and booked seats are never available again.- Confirming twice does nothing.
- Confirming an expired hold throws
HoldExpiredException. - Confirming a released hold throws
InvalidStateException.
- Release.
release(holdId)gives the seats back.- Releasing twice, or releasing an expired hold, does nothing.
- Releasing a confirmed hold throws
InvalidStateException.
- Query.
availableSeats(showId)returns the seats that are neither booked nor held by an unexpired hold. - Shows are independent. Seat
A1in one show has nothing to do withA1in another. - Config validation.
Solution.createthrowsIllegalArgumentExceptionfor any of these: no shows, duplicate show ids, a show with no seats, duplicate seats within a show, or a non-positive TTL.
Non-functional requirements
- No double booking, ever: a seat belongs to at most one live hold or booking.
- No deadlocks. User 1 asks for
[A1, A2]while user 2 asks for[A2, A1]. The tests do exactly this, with many threads and random orders. - A confirm that races with expiry, or with a release, must end in exactly one consistent outcome.
- Requests for different seats or shows should not block each other.
What you get
api/holds the contract the hidden tests use. It's read-only.Solution.create(config, clock)is where you build your system.Mainis your demo. The judge runs it.
The judge scores correctness (hidden tests, including multi-threaded ones) and design quality. Each counts for 50%.