AlgoViz

Design a movie ticket booking system

hard 90 min

Sell the numbered seats of a cinema: choose, hold for ten minutes, pay, cancel.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:35

See it run

Holds on a seat map: calls travel, owners change, the clock moves, and the matching lines light up.

Operations

Hold two seats

Check all, then write all, under the lock of the show.

Step 1 of 5

Eight o'clock

ownsnoorCustomerclock20:00officeBoxOfficeS1SeatMapB2freeB3freeB4freelockfree

Nobody has anything in show S1. A seat that is not in the map is free.

Step 1: Eight o'clock

def take(self, booking):    with self._lock:        lost = tuple(s for s in booking.seat_ids if self._owner(s))        if lost:            raise Refused(Reason.TAKEN, lost)        for seat_id in booking.seat_ids:            self._taken[seat_id] = booking
Cost
O(seats wished)
Guarantee
All or none
Seat by seat
needs an undo

Nothing written before all is checked: nothing to undo.

In the real world

The same movie ticket booking doing real jobs. Pick one and step through it.

Charged, and the answer is lost.

Step 1 of 4

Paying

leeCustomerclock20:14officeBoxOfficeS1SeatMapB2freeB3booking 3B4booking 3lockfreebooking 3BookingstatusPAYINGuntil20:20bankPaymentsbooking-33600charge(booking-3, 3600)

The charge is on its way.

Step 1: Paying

def pay(self, booking_id, customer):    booking = self._mine(booking_id, customer)    seats = self._map(booking.show_id)    if seats.start_paying(booking):        try:            self._payments.charge(f"booking-{booking.id}", booking.cents)        except Declined:            seats.end_paying(booking, paid=False)            raise        seats.end_paying(booking, paid=True)    return booking

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Status in the chair

    critical

    The room has two shows tonight. Sold for the first means sold for both.

    Wrong
    seat.status = BOOKED
    Right
    seat_map[show.id]._taken["B3"] = booking
  • Undo without an owner

    critical

    The undo frees a seat that somebody else has by now.

    Wrong
    except NotAvailable:    for s in reserved: s.release()
    Right
    lost = [s for s in wanted if self._owner(s)]if lost: raise Refused(Reason.TAKEN, lost)   # nothing written yet
  • A timer writes EXPIRED

    critical

    It races with the payment: charged, confirmed, and the seat is sold to another.

    Wrong
    schedule(lambda: booking.expire(), minutes=10)
    Right
    if booking.status is PENDING and booking.until <= now: return None
  • Timeout is not declined

    critical

    The customer was charged and has no seat.

    Wrong
    except Exception:    seats.end_paying(booking, paid=False)
    Right
    except Declined:    seats.end_paying(booking, paid=False)   # silence stays PAYING
  • A new key for each try

    major

    Every retry is a new charge.

    Wrong
    key = f"{booking.id}-{time.time()}"
    Right
    key = f"booking-{booking.id}"
  • Price from the browser

    major

    It is a wish. Somebody wishes for 1 cent.

    Wrong
    def hold(show, seats, total): ...
    Right
    cents = sum(pricing.cents(show, s) for s in seats)

Interview tips

  • Open with

    • The seat of a show.
    • All or none.
    • Who notices the end of a hold?
  • Reject aloud

    • No status in the chair.
    • No EXPIRED.
    • No lock held for ten minutes.
  • Prove it

    • Three customers, twelve minutes.
    • Two clicks on Pay.
    • A silent bank.
  • Never

    • The bank under the lock.
    • Charge first, look later.
    • A float for money.