Ticketmaster-like Concurrent Booking System with Seat Holds
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a Ticketmaster-like ticket booking system that supports concurrent seat reservations across multiple events.
Each event has a fixed number of seats. A user may hold a seat for a limited time, confirm the hold to complete the purchase, or release the hold. Holds expire automatically after a configurable duration. All operations must be thread-safe so that two users cannot purchase the same seat.
The system should also report the available seats for an event at any point in time.
Implement a class `Ticketmaster`, which starts with no events, with the following methods:
- `createEvent(eventId: int, capacity: int, price: int) -> bool`: Create a new event with the given seat capacity and per-seat price. Returns `False` if the event already exists.
- `holdSeat(eventId: int, seatNumber: int, userId: int, currentTime: int, holdDurationSeconds: int) -> str`: Attempt to place a temporary hold on a seat for a user. Returns a hold token string on success, or an empty string on failure (seat taken, held by another, or event missing).
- `confirmHold(eventId: int, seatNumber: int, holdToken: str, currentTime: int) -> int`: Confirm a held seat to complete the booking. Returns the price paid on success, or -1 if the hold is invalid or expired.
- `releaseHold(eventId: int, seatNumber: int, holdToken: str, currentTime: int) -> bool`: Release a seat that is currently held (but not confirmed) back to available. Returns `True` on success.
- `availableSeats(eventId: int, currentTime: int) -> int`: Return the number of seats currently available (not held or booked) for the event at the given time.
The exact token string is up to the implementation, so the traces below store each token in a variable.
Example 1, a hold that is confirmed:
```text
tm = Ticketmaster()
tm.createEvent(1, 3, 50) # True (event 1: seats 1 to 3, price 50)
tm.createEvent(1, 5, 80) # False (event 1 already exists)
a = tm.holdSeat(1, 2, 7, 100, 60) # non-empty token; user 7 holds seat 2 until time 160
tm.holdSeat(1, 2, 8, 110, 60) # "" (seat 2 is held by user 7)
tm.availableSeats(1, 120) # 2 (seat 2 is held)
tm.confirmHold(1, 2, a, 130) # 50 (hold is live and the token matches; seat 2 is booked)
tm.availableSeats(1, 140) # 2 (seat 2 is booked)
tm.releaseHold(1, 2, a, 150) # False (a booked seat cannot be released)
```
Example 2, a hold that expires:
```text
tm = Ticketmaster()
tm.createEvent(5, 2, 120) # True (event 5: seats 1 and 2, price 120)
b = tm.holdSeat(5, 1, 3, 0, 30) # non-empty token; the hold on seat 1 expires at time 30
tm.availableSeats(5, 29) # 1 (seat 1 is still held)
tm.availableSeats(5, 30) # 2 (the hold expired at time 30)
tm.confirmHold(5, 1, b, 31) # -1 (hold b has expired)
c = tm.holdSeat(5, 1, 4, 32, 30) # non-empty token; user 4 now holds seat 1
tm.releaseHold(5, 1, b, 33) # False (b belongs to the expired hold)
tm.releaseHold(5, 1, c, 34) # True (seat 1 is available again)
tm.holdSeat(5, 3, 4, 35, 30) # "" (event 5 has no seat 3)
tm.holdSeat(9, 1, 4, 36, 30) # "" (event 9 does not exist)
```
```hint Where the race is
Two calls for the same seat can both see it as available if checking the seat and updating it are separate steps. Decide which steps have to happen as one indivisible unit.
```
```hint Expiry without a timer
Every call already carries `currentTime`. Think about when a stale hold actually has to be noticed.
```
### Constraints & Assumptions
- `1 <= capacity <= 10^4`
- `1 <= eventId, seatNumber <= 10^4`
- `1 <= price <= 10^6`
- The seats of an event are numbered 1 to `capacity`. A call that names a missing event or a seat outside that range fails (`""`, -1 or `False`), and `availableSeats` returns 0 for a missing event.
- Times are integer seconds supplied by the caller. A hold placed at time `t` for `d` seconds is live while `currentTime < t + d` and has expired once `currentTime >= t + d`.
- A hold that is not confirmed expires after `holdDurationSeconds`.
- All operations must be thread-safe and behave atomically under concurrent access.
### Clarifying Questions to Ask
- Can the user who already holds a seat call `holdSeat` again to extend the hold, or does that fail like any other held seat?
- Can calls from different threads arrive with `currentTime` values out of order?
- Does the hold token only need to be unique, or also hard to guess?
- Can a booked seat ever be cancelled and returned to the pool?
### What a Strong Answer Covers
- The seat states, the allowed transitions between them, and what each method returns in each state.
- How checking a seat and updating it are made atomic, and how coarse or fine the locking is.
- How expired holds are detected and reclaimed, including the behavior exactly at the expiry time.
- How a hold token ties confirm and release to one specific hold.
- The time cost of each method and the memory cost of tracking seats.
### Follow-up Questions
- How would you make `availableSeats` faster than a scan of the event's seats while keeping expiry correct?
- How would you reduce lock contention when many users book different events at the same time?
- How would you let one user hold several seats as a single all-or-nothing operation?
- How would the design change if seat state lived in a database shared by several application servers?
Overview: Implement a thread-safe ticket booking class where users hold a seat for a limited time, confirm or release the hold, and ask how many seats are still free. It tests seat state modeling, atomic check-and-update under concurrent access, hold expiry and token-based ownership checks.