Design a Movie Ticket Booking System
You'll learn to
- -Explain why a seat needs a timed hold between "selected" and "booked", unlike a parking spot
- -Model the seat lifecycle as a State machine: available, locked with an expiry, booked
- -Run the lifecycle, including an expired hold and five simultaneous clicks on one seat
This prompt reuses the concurrency lesson from the parking lot and adds a new wrinkle: a seat must be held for a while during payment, not claimed instantly. If it were claimed the instant someone clicked, a person who wandered off during checkout would block that seat for everybody, forever. Think of the little "held for you for 5 minutes" timer on any ticket site.
Step 1: Core Entities
The nouns are a seat, a show (one screening, with its set of seats) and a booking service. A seat knows its own state, which starts as available.
class Seat:
def __init__(self, seat_id: str):
self.seat_id = seat_id
self.state: "SeatState" = AvailableState()
class Show:
def __init__(self, show_id: str, movie: str, seats: list[Seat]):
self.show_id = show_id
self.movie = movie
self.seats = {s.seat_id: s for s in seats}Step 2: The Seat Lifecycle as a State Machine
A state machine is a set of named situations with rules for moving between them. A seat goes Available, then Locked (held during checkout, with an expiry time), then Booked once payment is confirmed. It returns to Available if the hold expires or checkout is canceled. This is the State pattern, with the extra twist that one transition happens by itself when time runs out.
Two details deserve attention in the code below. When an old hold has expired and someone tries to lock the seat, the new user should get it straight away, not be told no. And confirm takes the user's id, so only the person who holds the seat can pay for it.
import time
class SeatState(ABC):
@abstractmethod
def lock(self, seat: Seat, user_id: str) -> bool: ...
@abstractmethod
def confirm(self, seat: Seat, user_id: str) -> bool: ...
@abstractmethod
def release(self, seat: Seat) -> None: ...
class AvailableState(SeatState):
def lock(self, seat: Seat, user_id: str) -> bool:
seat.state = LockedState(user_id, expires_at=time.time() + 300) # 5-minute hold
return True
def confirm(self, seat: Seat, user_id: str) -> bool: return False
def release(self, seat: Seat) -> None: pass
class LockedState(SeatState):
def __init__(self, user_id: str, expires_at: float):
self.user_id = user_id
self.expires_at = expires_at
def _check_expiry(self, seat: Seat) -> None:
if time.time() > self.expires_at:
seat.state = AvailableState()
def lock(self, seat: Seat, user_id: str) -> bool:
self._check_expiry(seat)
if isinstance(seat.state, AvailableState): # the old hold had expired
return seat.state.lock(seat, user_id)
return False # still held (by this or another user)
def confirm(self, seat: Seat, user_id: str) -> bool:
self._check_expiry(seat)
if isinstance(seat.state, LockedState) and seat.state.user_id == user_id:
seat.state = BookedState() # only the user who holds the seat can confirm it
return True
return False # expired, or held by someone else
def release(self, seat: Seat) -> None:
seat.state = AvailableState()
class BookedState(SeatState):
def lock(self, seat: Seat, user_id: str) -> bool: return False
def confirm(self, seat: Seat, user_id: str) -> bool: return False
def release(self, seat: Seat) -> None: pass # a real system: only via a cancellation flowStep 3: Concurrency-Safe Locking
As with the parking lot, locking a seat must be one atomic step guarded by a lock, because two people clicking the same seat in the same instant must never both succeed. The BookingService wraps each call in a lock. A per-seat or per-show lock is a refinement, and one lock for the whole service is the simple correct start.
import threading
class BookingService:
def __init__(self):
self._lock = threading.Lock()
def hold_seat(self, show: Show, seat_id: str, user_id: str) -> bool:
with self._lock:
seat = show.seats[seat_id]
return seat.state.lock(seat, user_id)
def confirm_booking(self, show: Show, seat_id: str, user_id: str) -> bool:
with self._lock:
seat = show.seats[seat_id]
return seat.state.confirm(seat, user_id)Step 4: Expiring Abandoned Holds
The design checks expiry lazily, only when lock or confirm is next called on that seat, instead of running a timer per seat. That is a deliberate simplicity trade-off worth naming. A seat whose hold has actually expired may still look locked to a status check until someone touches it. A background sweep is the natural follow-up if the interviewer pushes.
Step 5: Run It
The demo uses a fake clock so you can read exact times. Alice holds and confirms seat A1 while Bob is turned away. Carol holds A2 but is too slow, and Dave takes it after her five minutes pass. Finally five users click A3 at the same moment.
A subtle bug to avoid: confirm must re-check expiry itself, not only lock. Otherwise a payment that completes a moment after the hold expired would succeed on a seat that has already been released. It must also check who holds the seat, or one person's late payment could confirm another person's hold.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Forge Level 47: Movie Ticket Booking System asks you to implement seat booking across shows (book_seat()/get_available_seats()), graded by running your actual code against real test cases.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!