LLD: movie ticket booking
45 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)Two people press "Book" on seat F7 of the same show at the same moment, and only one may get it. The round is about holding seats safely while they pay, not about the screens.
Requirements: cities, theatres, shows, seats and bookings
A show seat is one seat of one show, and it carries the free or taken status. The same physical seat is a different show seat in every show.
The system must never sell one seat of one show twice. This is the scope to agree on.
City 1 -- * Theatre 1 -- * Screen 1 -- * Show (movie, start time)Show 1 -- * ShowSeat (seat, type, status) Booking -> Show, seats, user, statusBrowse: shows by city and movieHold: pick 1-6 seats; all are held together or none; hold lasts 10 minutesPay: confirm the hold before it expires; expired holds free the seatsCancel: a confirmed booking can be cancelled; seats become freePrice: by seat type (Silver, Gold, Recliner) and show time (morning cheaper)Must not: sell one seat of one show twiceUsers browse shows by city and theatre, see the seat map, pick seats and pay.
Ask two questions that shape the design: how long are seats held while the user pays (usually a few minutes), and can one booking have several seats (yes, and then all of them must be held together, or none).
Seat locking to stop double booking
A seat hold marks seats as taken for a short time while one user pays, so nobody else can pick them. A hold is all-or-nothing: either every requested seat is held, or none.
Two examples: a hold that takes all the seats or none, then twenty users competing for one seat.
Example 1 · All-or-nothing seat hold
import java.util.HashMap;import java.util.List;import java.util.Map;
class Show { private final String name; private final Map<String, String> heldBy = new HashMap<>(); // seat -> user
Show(String name) { this.name = name; }
// one step: check every seat, then hold every seat synchronized boolean hold(String user, List<String> seats) { for (String seat : seats) { if (heldBy.containsKey(seat)) return false; } for (String seat : seats) { heldBy.put(seat, user); } return true; }
synchronized String owner(String seat) { return heldBy.getOrDefault(seat, "free"); }}
public class SeatHoldDemo { public static void main(String[] args) { Show show = new Show("Leo 7:00 PM"); System.out.println("Priya F6-F8: " + show.hold("Priya", List.of("F6", "F7", "F8"))); System.out.println("Karan F8-F9: " + show.hold("Karan", List.of("F8", "F9"))); System.out.println("Karan F9-F10: " + show.hold("Karan", List.of("F9", "F10"))); for (String seat : List.of("F7", "F8", "F9", "F11")) { System.out.println(seat + " -> " + show.owner(seat)); } }}Priya F6-F8: true Karan F8-F9: false Karan F9-F10: true F7 -> Priya F8 -> Priya F9 -> Karan F11 -> free
What this code does
- Karan's first request fails as a whole because F8 is Priya's, so F9 is not held either. His second request succeeds.
- The method checks every seat before it changes anything, so a failed request never leaves half its seats held.
synchronizedon the show means two requests for the same show run one after the other; the next example puts that to the test.
Example 2 · Twenty users, one seat
import java.util.HashMap;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;
class Show { private final Map<String, String> heldBy = new HashMap<>();
synchronized boolean hold(String user, String seat) { if (heldBy.containsKey(seat)) return false; heldBy.put(seat, user); return true; }}
public class SeatRaceDemo { public static void main(String[] args) throws InterruptedException { Show show = new Show(); AtomicInteger winners = new AtomicInteger(); AtomicInteger losers = new AtomicInteger();
ExecutorService pool = Executors.newFixedThreadPool(8); for (int i = 1; i <= 20; i++) { String user = "user" + i; pool.submit(() -> { if (show.hold(user, "F7")) winners.incrementAndGet(); else losers.incrementAndGet(); }); } pool.shutdown(); pool.awaitTermination(5, TimeUnit.SECONDS);
System.out.println("Got F7: " + winners.get()); System.out.println("Told \"seat just taken\": " + losers.get()); }}Got F7: 1 Told "seat just taken": 19
What this code does
- Eight threads act as eight server threads handling twenty clicks at once. Who wins changes each run; that exactly one wins never does.
- A plain
HashMapis safe here only because every access goes through thesynchronizedmethod. - Without
synchronized, two threads could both see F7 missing from the map and both put it, so two people would pay for one seat.
If a user asks for F6, F7 and F8 and F7 is gone, none of the three is held. Two users can press "Book" at the same moment, each on their own server thread; if both check "is F7 free?" before either marks it, both succeed. Making "check every seat, then hold every seat" one synchronized step on the show lets only one thread in at a time.
Three rules for a safe hold:
- Lock per show, not for the whole system: users of different shows never wait for each other.
- Check all seats first, hold them only if every one is free.
- Give every hold an expiry time, so a user who closes the app does not block seats forever.
Find the bug
Find the unsafe hold
Click the line that breaks the hold.
A request for F7 and F8 must hold both seats or neither.
Round 1 of 2Pricing strategy by seat type and show time
A pricing strategy is the rule that turns a seat and a show into a price. The booking service asks it for a number and never decides prices itself.
Prices come from one table by seat type, with time rules on top.
Example 3 · Pricing by seat type and show time
import java.time.LocalDateTime;import java.util.Map;
enum SeatType { SILVER, GOLD, RECLINER }
interface PricingStrategy { int price(SeatType type, LocalDateTime showTime);}
class StandardPricing implements PricingStrategy { private final Map<SeatType, Integer> base = Map.of(SeatType.SILVER, 150, SeatType.GOLD, 220, SeatType.RECLINER, 400);
public int price(SeatType type, LocalDateTime showTime) { int price = base.get(type); if (showTime.getHour() < 12) { price = price * 80 / 100; // morning shows: 20% off } return price; }}
public class PricingDemo { public static void main(String[] args) { PricingStrategy pricing = new StandardPricing(); LocalDateTime morning = LocalDateTime.of(2026, 10, 18, 9, 30); LocalDateTime evening = LocalDateTime.of(2026, 10, 18, 19, 0);
for (SeatType type : SeatType.values()) { System.out.println(type + ": morning Rs " + pricing.price(type, morning) + ", evening Rs " + pricing.price(type, evening)); } int total = 2 * pricing.price(SeatType.GOLD, evening) + pricing.price(SeatType.RECLINER, evening); System.out.println("2 Gold + 1 Recliner, evening: Rs " + total); }}SILVER: morning Rs 120, evening Rs 150 GOLD: morning Rs 176, evening Rs 220 RECLINER: morning Rs 320, evening Rs 400 2 Gold + 1 Recliner, evening: Rs 840
What this code does
- Base prices live in one map; the morning rule is applied on top in the same strategy.
price * 80 / 100multiplies first, so integer division does not lose the discount (150 × 80 / 100 = 120).- A festival offer would be another
PricingStrategy, possibly wrapping this one, with no change to booking code.
Cinemas change prices often (morning discounts, weekend rates, festival offers), so the rule sits behind a PricingStrategy interface.
Prices here are whole rupees to keep the code short; a real system stores money as BigDecimal or as paise in a long.
Booking states: held, confirmed, cancelled
A booking state says where a booking is in its life: HELD, CONFIRMED, CANCELLED or EXPIRED. Only some moves between states are allowed.
One booking tries the allowed moves and the rejected ones.
Example 4 · Booking states and invalid moves
enum BookingStatus { HELD, CONFIRMED, CANCELLED, EXPIRED }
class Booking { private final String id; private BookingStatus status = BookingStatus.HELD;
Booking(String id) { this.id = id; }
void confirm() { move(BookingStatus.HELD, BookingStatus.CONFIRMED); } void expire() { move(BookingStatus.HELD, BookingStatus.EXPIRED); } void cancel() { move(BookingStatus.CONFIRMED, BookingStatus.CANCELLED); }
private void move(BookingStatus from, BookingStatus to) { if (status != from) { throw new IllegalStateException(id + ": cannot go " + status + " -> " + to); } status = to; }
BookingStatus status() { return status; }}
public class BookingStatesDemo { static void attempt(String label, Runnable action) { try { action.run(); System.out.println(label + ": ok"); } catch (IllegalStateException e) { System.out.println(label + ": " + e.getMessage()); } }
public static void main(String[] args) { Booking b1 = new Booking("BK1"); attempt("BK1 confirm", b1::confirm); attempt("BK1 cancel", b1::cancel); attempt("BK1 cancel again", b1::cancel);
Booking b2 = new Booking("BK2"); attempt("BK2 expire", b2::expire); attempt("BK2 late payment", b2::confirm); System.out.println("Final: BK1 " + b1.status() + ", BK2 " + b2.status()); }}BK1 confirm: ok BK1 cancel: ok BK1 cancel again: BK1: cannot go CANCELLED -> CANCELLED BK2 expire: ok BK2 late payment: BK2: cannot go EXPIRED -> CONFIRMED Final: BK1 CANCELLED, BK2 EXPIRED
What this code does
- Each public method names one allowed move;
movechecks the current state before changing it. - A late payment on an expired hold throws, which is what stops a seat that was already released from being sold twice.
- This is the simple form of the State idea. The vending machine lesson shows the full State pattern with one class per state.
The allowed moves are HELD → CONFIRMED, HELD → EXPIRED and CONFIRMED → CANCELLED. Put them inside the booking and throw an exception for any other move.
Then a late payment on an expired hold, or a cancel on an already cancelled booking, fails loudly instead of quietly selling a seat twice.
Predict the output
Which moves are allowed?
Read the rule once, then predict each pair of calls.
Round 1 of 2
Single-file design with a demo run
Lazy expiry means a hold is treated as expired when it is checked after its expiry time, so no timer thread is needed.
The whole booking system in one file, with the time passed in so the output never changes.
Example 5 · The booking system in one file
import java.time.LocalDateTime;import java.util.HashMap;import java.util.List;import java.util.Map;
enum SeatType { SILVER, GOLD }enum BookingStatus { HELD, CONFIRMED, CANCELLED, EXPIRED }
interface PricingStrategy { int price(SeatType type, LocalDateTime showTime); }
class StandardPricing implements PricingStrategy { public int price(SeatType type, LocalDateTime showTime) { int base = type == SeatType.GOLD ? 220 : 150; return showTime.getHour() < 12 ? base * 80 / 100 : base; }}
class Booking { final String id, user; final List<String> seats; final int amount; final LocalDateTime expiresAt; BookingStatus status = BookingStatus.HELD; Booking(String id, String user, List<String> seats, int amount, LocalDateTime expiresAt) { this.id = id; this.user = user; this.seats = seats; this.amount = amount; this.expiresAt = expiresAt; }}
class Show { final String title; final LocalDateTime start; final Map<String, SeatType> seatTypes; private final Map<String, Booking> taken = new HashMap<>(); // seat -> booking
Show(String title, LocalDateTime start, Map<String, SeatType> seatTypes) { this.title = title; this.start = start; this.seatTypes = seatTypes; }
private boolean isFree(String seat, LocalDateTime now) { Booking b = taken.get(seat); if (b == null || b.status == BookingStatus.CANCELLED || b.status == BookingStatus.EXPIRED) return true; if (b.status == BookingStatus.HELD && now.isAfter(b.expiresAt)) { b.status = BookingStatus.EXPIRED; // lazy expiry: checked when someone asks return true; } return false; }
synchronized boolean tryHold(Booking booking, LocalDateTime now) { for (String seat : booking.seats) if (!isFree(seat, now)) return false; for (String seat : booking.seats) taken.put(seat, booking); return true; }}
class BookingService { private final PricingStrategy pricing; private int next = 1; BookingService(PricingStrategy pricing) { this.pricing = pricing; }
Booking hold(Show show, String user, List<String> seats, LocalDateTime now) { int amount = seats.stream().mapToInt(s -> pricing.price(show.seatTypes.get(s), show.start)).sum(); Booking booking = new Booking("BK" + next, user, seats, amount, now.plusMinutes(10)); boolean ok = show.tryHold(booking, now); if (ok) next++; System.out.println(user + " holds " + seats + ": " + (ok ? booking.id + ", Rs " + amount : "seats not available")); return ok ? booking : null; }
void confirm(Show show, Booking booking, LocalDateTime paidAt) { synchronized (show) { if (booking.status == BookingStatus.HELD && !paidAt.isAfter(booking.expiresAt)) { booking.status = BookingStatus.CONFIRMED; } else if (booking.status == BookingStatus.HELD) { booking.status = BookingStatus.EXPIRED; } } System.out.println(booking.user + " pays for " + booking.id + ": " + booking.status); }}
public class MovieBookingDemo { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2026, 10, 18, 19, 0); Show show = new Show("Leo", start, Map.of("F6", SeatType.SILVER, "F7", SeatType.SILVER, "A1", SeatType.GOLD, "A2", SeatType.GOLD)); BookingService service = new BookingService(new StandardPricing()); LocalDateTime t = LocalDateTime.of(2026, 10, 18, 12, 0);
Booking priya = service.hold(show, "Priya", List.of("A1", "A2"), t); Booking karan = service.hold(show, "Karan", List.of("F7", "A2"), t.plusMinutes(1)); Booking meera = service.hold(show, "Meera", List.of("F6", "F7"), t.plusMinutes(2)); service.confirm(show, priya, t.plusMinutes(4)); service.confirm(show, meera, t.plusMinutes(13)); Booking karan2 = service.hold(show, "Karan", List.of("F6", "F7"), t.plusMinutes(14)); service.confirm(show, karan2, t.plusMinutes(15)); }}Priya holds [A1, A2]: BK1, Rs 440 Karan holds [F7, A2]: seats not available Meera holds [F6, F7]: BK2, Rs 300 Priya pays for BK1: CONFIRMED Meera pays for BK2: EXPIRED Karan holds [F6, F7]: BK3, Rs 300 Karan pays for BK3: CONFIRMED
What this code does
- Priya holds and pays in time. Karan's first request fails because A2 is Priya's, so neither of his seats is held.
- Meera pays 11 minutes after her hold, after it expired, so her booking becomes
EXPIREDand her seats are free again. Karan then gets F6 and F7. - Expiry is "lazy": no timer thread runs; a hold is treated as expired when someone checks it after
expiresAt. A real system also runs a small cleanup job.
Show owns its seat map and the synchronized hold method, PricingStrategy prices the hold, Booking guards its own states, and BookingService ties them together: hold, confirm with payment, expire, cancel.
Passing the time in as a parameter instead of calling now() makes expiry easy to show (pass a time 11 minutes later) and keeps the output the same on every run.
Next step
Check your understanding
12 quick questions