Skip to main content

Summary & Key Takeaways

Editorial

Design Recap

We designed a Movie Ticket Booking System from requirements to thread-safe implementation. Here's what we built:

Domain Model (15 types):

LayerClassesEnumsInterfaces
VenueTheatre, Screen, SeatSeatType—
SchedulingMovie, Show, ShowSeatShowSeatStatus, Genre, Language—
BookingBooking, Payment, UserBookingStatus, PaymentStatus, PaymentMethod—
ServicesBookingService—PricingStrategy, NotificationService

Key architectural decisions:

  1. ShowSeat bridge class — The most important design choice. It links a physical Seat to a specific Show, enabling per-show seat availability tracking. Without it, "seat 5C" would have no show context.

  2. Two-phase booking — Phase 1 (Hold): seats are temporarily reserved, booking is PENDING. Phase 2 (Pay): payment is processed, booking becomes CONFIRMED. This prevents users from paying for seats that are already taken.

  3. Composition over inheritance — Theatre composes Screens, Screen composes Seats, Show composes ShowSeats, Booking composes Payment. No inheritance hierarchies in the domain model — just clean composition.

  4. Per-show locking — Thread safety at the Show level balances correctness (no double bookings) with performance (users in different shows proceed in parallel).

UML Diagrams Summary

We created four UML diagram types — more than most design problems because of the system's complexity:

1. Use Case Diagram

  • 3 actors: Customer (primary), Admin, System Timer
  • 11 use cases covering browsing, booking, payment, cancellation, and administration
  • Key insight: System Timer as an actor — automated hold expiration is a first-class concern

2. Class Diagram

  • 8 classes, 5 enums, 2 interfaces
  • 3 composition relationships (Theatre→Screen, Screen→Seat, Show→ShowSeat)
  • ShowSeat as the bridge between static venue structure and dynamic booking state

3. Sequence Diagram — Book a Ticket

  • 8 participants in the two-phase booking flow
  • 18 messages covering the happy path: hold seats → calculate price → process payment → confirm
  • 3 alt fragments for failure scenarios: seat unavailable, hold expired, payment failed

4. State Machine Diagram — Booking Status

  • 4 booking states: PENDING, CONFIRMED, CANCELLED, EXPIRED
  • 6 transitions with events, guards, and actions
  • Coupled ShowSeat sub-state machine: AVAILABLE → HELD → BOOKED
  • 2 terminal states (CANCELLED, EXPIRED) — no recovery, user must create a new booking

Design Patterns Summary

PatternWhereWhat It SolvesSOLID Principle
StrategyPricingStrategy → StandardPricing, PeakHourPricingDifferent pricing formulas without modifying BookingServiceOpen/Closed
ObserverNotificationService → ConsoleNotificationServiceDecouples booking events from notification channelsSingle Responsibility, Dependency Inversion
StateBookingStatus state machine (implicit via enum + transitions)Booking behavior varies by state with strict transition rulesSingle Responsibility

Pattern interaction: The BookingService orchestrates all three — Strategy calculates how much, Observer notifies whom, State determines what's allowed. Each pattern removes one dimension of complexity, keeping the service focused on workflow coordination.

Concurrency Summary

The core problem: Multiple users booking the same seat at the same time — a TOCTOU race condition where check available → hold seat is not atomic.

Our solution: Per-show ReentrantLock / threading.Lock that serializes all seat operations for a given show.

Why per-show, not per-seat or global?

  • Global lock: Too restrictive — users booking different shows shouldn't wait for each other
  • Per-seat lock: Deadlock risk when booking multiple seats (Alice locks A1 → A3, Bob locks A3 → A1)
  • Per-show lock: Sweet spot — users in different shows proceed in parallel; within a show, operations are serialized

Production alternative: In a database-backed system, optimistic locking (version columns on ShowSeat rows) replaces in-memory locks and scales across multiple application servers.

Interview Tips

Time allocation (45 minutes):

  • 5 min: Clarify requirements (R1-R13 scope, in/out of scope)
  • 5 min: Identify key classes via noun extraction
  • 10 min: Draw the class diagram (start with ShowSeat — it's the star of the design)
  • 5 min: Walk through the booking sequence (two-phase flow)
  • 5 min: State machine (BookingStatus + ShowSeatStatus)
  • 10 min: Code the core (Booking, ShowSeat, BookingService)
  • 5 min: Discuss patterns, concurrency, extensions

What interviewers look for in this problem:

  1. ShowSeat bridge class — If you don't introduce this, you can't model per-show seat availability. This is the make-or-break class.
  2. Two-phase booking — Hold first, pay second. If you try to do it in one step, you'll struggle with concurrent access and timeouts.
  3. State machine awareness — Can you explain which transitions are valid and which guards protect them?
  4. Concurrency thinking — Even if you don't implement locks, mentioning the double-booking race condition shows awareness.

Common mistakes to avoid:

  • Skipping ShowSeat and putting availability directly on Seat (doesn't handle multiple shows)
  • Forgetting hold timeout (seats stay locked forever)
  • Making Booking directly modify Seat (wrong direction — ShowSeat is the bridge)
  • Not separating Payment from Booking (SRP violation)

Extensions to Explore

If the interviewer asks "how would you extend this?", here are well-prepared answers:

1. Waitlist / Auto-Booking on Cancellation

  • When a show is sold out, allow users to join a waitlist
  • When a booking is cancelled, automatically notify the first waitlisted user
  • Pattern: Observer (waitlist observes cancellation events)
  • Related: DP30 Course Registration waitlist uses the same approach

2. Dynamic Pricing Based on Demand

  • Track fill rate per show: as seats fill, prices increase
  • Implement as a new PricingStrategy that reads live availability
  • No changes to BookingService — just inject a different strategy

3. Seat Selection UI with Real-Time Availability

  • Seats change status (AVAILABLE → HELD → BOOKED) while the user browses
  • Use WebSockets to push ShowSeat status changes to connected clients
  • The Observer pattern naturally extends: ShowSeat changes → notify UI clients

4. Multi-Theatre Search and Aggregation

  • Search for a movie across all theatres in a city
  • Requires a MovieSearchService that queries Shows across multiple Theatres
  • Pattern: Repository pattern for Show/Theatre lookups

5. Refund Policy as a Strategy

  • Full refund if cancelled 2+ hours before show, 50% within 2 hours, no refund after
  • Implement as a RefundStrategy interface — similar to PricingStrategy
  • Clean extension of the existing Strategy usage

Cross-References

This problem connects to several others in the course:

Related ProblemConnection
DP14: Hotel Reservation (Medium)Same two-phase booking pattern (hold → confirm), room status state machine parallels ShowSeat states
DP15: Parking Lot (Medium)Spot assignment is analogous to seat selection — ParkingSpot maps to ShowSeat
DP6: Vending Machine (Easy)State machine with timeout — VendingMachine's coin return timeout parallels our hold timeout
DP11: ATM System (Medium)State machine with guards — ATM's PIN validation guard parallels our hold-not-expired guard
DP31: Notification System (Hard)Our NotificationService is a simplified version of the full Notification System design
DP35: Rate Limiter (Concurrency)Strategy pattern for algorithm selection (Token Bucket vs. Sliding Window) parallels our pricing strategy

Fundamentals prerequisites used:

  • S4: OOP Relationships (Composition for Theatre→Screen→Seat)
  • S5: UML Class Diagrams (15-type domain model)
  • S6: SOLID Principles (OCP via Strategy, SRP via Payment separation)
  • S11: Observer Pattern (NotificationService)
  • S12: Strategy and State Patterns (PricingStrategy, BookingStatus lifecycle)