CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

Payment failure, expiry and scale.

Step 5: edge cases, tests and change requests

Edge cases:

Case Behaviour
Two buyers want the last seat One wins. The other gets SeatTaken
Buyer pays after the hold expired Refused, and the seats are freed
Payment fails after confirm Real systems confirm only after payment succeeds, or undo the booking (a compensating action, as in Level 6, module 5)
Buyer confirms twice The second call finds no hold and fails
Empty seat set Refused
Show already started Add a check against the start time
Server restarts Holds in memory are lost. A database with an expiry column fixes this

A weakness worth naming: the hold in Cinema and the state in SeatMap are two places. If the process stops between them, they can disagree. In a real system both live in one database transaction, and the "check then take" is a single conditional update, such as "set status to held where status is free". The in-memory code shows the logic. The database makes it durable.

Tests to write first: each row of the run table; the race with 100 callers; a double confirm; the sweep; and a total that mixes rows.

Change requests and the part that changes:

  • "Discount for students, or a weekend surcharge." A new PriceRule wrapper.
  • "Choose the best available seats together." A seat-picking strategy that finds a block of adjacent seats.
  • "Several cinemas and cities." A Cinema per site and a search layer on top.
  • "Cancel a booking." Release the seats and refund by the rule.
  • "Notify the buyer by email." An observer on booking events.
  • "Different seat types, such as recliners." A seat category field and a price rule that reads it.
  • "Many servers." The seat state moves to a shared database. A conditional update replaces the lock.

Common mistake

Checking that a seat is free in one step and marking it taken in another. Two buyers pass the check together, and both get the seat. The check and the mark must be one step under one lock, or one conditional update in a database.

Interview angle

Answer in your head first, then open it.

How do you stop two people booking one seat?

One owner of seat state, with check and take in a single synchronized step, or a conditional update in a database.

Why a hold?

The buyer needs time to pay, and the seats must not be sold in that time. The hold expires so seats do not stay locked forever.

How is a multi-seat booking all-or-nothing?

Check every seat first, then take them all under one lock.

How do prices combine?

A strategy interface with decorators.

Check yourself: 1. Which change request is only a new price wrapper? 2. Why does a restart lose holds in the in-memory design? 3. What does the database do that the lock did?

Words you will meet

Word Meaning
Show A movie at a time on one screen
Seat map The grid of seats for one show and their states
Hold A timed claim on seats
Booking A confirmed purchase of seats
Expiry The moment a hold stops being valid