Design a movie ticket booking system with shows, seats and a temporary hold that expires if the user does not pay. Write the classes.
A ticket booking design models a Show that owns its seat states and a Hold with an owner, seat set and expiry. Holds are taken all-or-nothing under a per-show lock, expiry is checked lazily so no timer decides correctness, and starting payment moves the hold to a state the timer can no longer release.
Answer
Short answer
Treat the hold as a first-class object. A movie ticket booking system models a Show that owns the state of its seats and a Hold with an owner, a set of seats, an expiry and its own lifecycle. A seat hold is taken all-or-nothing under the show's lock, a seat counts as free if its hold has expired even before anything cleans it up, and starting payment moves the hold into a state that expiry no longer touches. Confirm turns held seats into booked seats; release or expiry makes them available again.
Was this answer helpful?
Likely follow-ups
- The user double-clicks Hold and two identical requests arrive. What does your second call return?
- How would you cap each user at ten held seats per show without a second lock?
- The payment callback never arrives. Which component notices, after how long, and what does it do to the seats?
- Theatres want to block a row for wheelchair access for some shows only. Where does that rule live?
Related questions
- Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?hardAlso on machine-coding and concurrency6 min
- Design a food-ordering system: customers place orders with restaurants, and the system assigns a delivery partner. Walk me through the classes.mediumAlso on state-machine and machine-coding5 min
- Design an elevator system for a building with N lifts. When someone presses the call button, which lift answers, and where in your code is that decided?hardAlso on state-machine and machine-coding6 min
- Design an in-memory job scheduler that runs one-off, delayed and recurring jobs. Write the core classes.hardAlso on machine-coding and concurrency6 min