The interview problem
Design a waitlist for sold-out classes. Customers request one to four seats together. When seats become available, offer them to waiting customers for ten minutes. Support acceptance, withdrawal, expiry and duplicate notifications without overbooking. Explain what “fair” means when the oldest group does not fit.
Original system-design practice, not a reported company interview. Workload and calculations are hypothetical. Official technical reference retrieved September 27, 2026; documentation publication date and interview date are not established or applicable.
1. Choose a fairness contract before a queue
Use strict FIFO within each class for this baseline: order by a sequence allocated under the class's transactional lock when joining commits. A group is indivisible. New purchases cannot bypass the waitlist. If the oldest eligible group does not fit, leave available seats idle until enough seats accumulate or that customer withdraws. Tell the customer that position is not a guaranteed time estimate.
Suppose three seats are free and the queue is A requesting four, B requesting two, then C requesting one. Strict FIFO offers nothing. A fit-first policy could offer B and C immediately, but repeated small groups could starve A. An alternative explicitly limits bypasses and then reserves accumulating capacity for A. These are different product policies: do not advertise strict FIFO while silently skipping large groups.
Reject a group larger than total class capacity at entry. Define eligibility changes, disabled accounts and the deadline after which no new offer can be useful. An offer that expires removes that entry from the queue; rejoining creates a new sequence. Otherwise an absent customer could repeatedly block everyone.
2. Keep capacity and offers in one consistency boundary
join / withdraw / accept API
↓
class-scoped transactional allocator
↓
class capacity + queue entries + offers + bookings + outbox
↓ ↓
due-offer sweeper notification workers
↓ ↓
re-run allocation email / pushFor each class, maintain total capacity C, confirmed seats B and offered seats H. Enforce B + H ≤ C. A waiting entry has customer, quantity, queue sequence and state. An offer has an immutable ID, generation, quantity, deadline and state. Count every outstanding ACTIVE offer in H until a transaction releases or confirms it, even if its deadline has passed. This conservative accounting may delay reuse but cannot release the same seats twice.
All joins, allocations, acceptances, withdrawals, booking cancellations and capacity changes acquire the same class row lock first, then related rows in a consistent order. After the lock is acquired, read authoritative state afresh. A single database transaction updates the relevant rows and counters. Unique keys enforce one active entry per customer/class and idempotent request handling. Validate request parameters when reusing an idempotency key.
Release and allocation can happen in the same transaction: expire due offers, return their held seats, select the oldest eligible waiting entry, and create an offer if its whole quantity fits. Repeat only for a bounded number of entries, then persist a continuation obligation. Notification events enter an outbox in that transaction. Sending an email is never the act that reserves capacity.
3. Resolve acceptance versus expiry
Define acceptance as the allocator's successful state transition, not the client's click time or notification receipt time. After acquiring the class lock, obtain current authoritative database time. Accept only if the offer is ACTIVE and this time is strictly before its deadline. At equality, expiry wins by policy. Avoid using a transaction-start timestamp captured before a long lock wait.
Acceptance moves q seats from H to B and creates one booking atomically. Expiry moves q seats from H back to free capacity and marks the offer EXPIRED. Both serialize on the same class and offer. If acceptance wins while eligible, the later sweeper sees ACCEPTED and releases nothing. If expiry wins, a late acceptance cannot revive that offer, even if the user has an old email open.
For C = 20, B = 16 and an offer for four seats, H = 4 and free capacity is zero. Acceptance yields B = 20, H = 0. Expiry instead yields B = 16, H = 0 and four reusable seats. An expiry retry does not return another four. Cancellation of the resulting booking uses the booking state and returns capacity once; it must not also cancel the already-consumed offer.
Commit may occur just after the deadline even though the guarded transition occurred before it. That is allowed by this stated contract; another allocator cannot consume those seats while the transaction holds the lock. Keep transactions short and report success only after commit. On an ambiguous response, retrieve the persisted outcome using the original request ID.
4. Separate delivery reliability from allocation fairness
The baseline ten-minute deadline starts at offer creation. Notification delay therefore reduces usable response time. Track creation-to-provider-acceptance and, where available, delivery delay. Show the authoritative offer and remaining time in the website. Retried notifications carry the same offer ID; an old notification opens a page that checks its current status instead of presenting a new acceptance capability.
If the business requires ten minutes after confirmed delivery, design an additional bounded delivery-pending hold and an explicit delivery signal. Email provider acceptance is not proof a human received the message. An unbounded pending hold can consume capacity forever. A reasonable compromise is a maximum pending interval, a supported channel acknowledgment and a documented failure policy.
Outbox retries and expiry scanning are durable and replayable. A process crash must not leave an offer dependent on an in-memory timer. Acceptance validates time directly, so a delayed sweeper cannot make an expired offer acceptable. Expiry cleanup eventually makes its capacity reusable.
5. Calculate throughput and holding cost
Assume 12,000 seats enter offers per hour. Of these offered seats, 30% are accepted after two minutes and 70% expire after ten minutes. The mean held duration is 0.3 × 2 + 0.7 × 10 = 7.6 minutes. Under a stable workload, average held seats are 12,000 × 7.6/60 = 1,520. This calculation counts offered seat episodes, including a seat offered again after expiry; it is not the number of distinct physical seats released per hour.
Shortening expiry to five minutes would give 4.1 minutes and 820 average held seats only if the acceptance fraction and two-minute accepted duration stayed unchanged. That saves 700 held seats in this hypothetical comparison, but a shorter window can reduce acceptance and accessibility. Measure the behavioral change rather than treating the savings as guaranteed.
A hot class with a measured five-millisecond serialized allocator transaction has a rough upper bound of 200 transactions/second before other overhead. Increasing stateless API workers cannot remove that serialized bottleneck. Queue bursts, batch bounded allocation work and partition independent classes. Splitting one class across regions needs disjoint capacity rights and a new fairness definition; global FIFO is not preserved automatically.
If 60,000 offers expire together and the expiry path sustains 1,000 completed transitions/second, cleanup requires at least 60 seconds without new work or retries. Timely expiry eligibility remains safe, but capacity reuse is late. Provision for bursts, stagger offer creation where acceptable and measure oldest unprocessed expiry rather than average worker latency.
6. Test failure and policy boundaries
Test duplicate release events, accept-versus-expire, booking-cancel retries, withdrawal during allocation, a notification arriving after replacement, and a crash after commit before acknowledgment. Reconcile counters against booking and offer rows, alert on disagreements and stop new allocation for a corrupted class. Reconciliation is a recovery mechanism, not a substitute for transactional updates.
Do not reduce class capacity below B + H with an ordinary administrative write. Require an explicit disruption workflow to revoke offers or resolve existing bookings, with notifications and audit history. When the database cannot establish authority, delay acceptance instead of issuing competing local promises.
Database time gives one decision authority, not immunity from clock faults. A large forward jump can expire offers early in real elapsed time. Monitor clock health, document whether deadlines follow displayed UTC or elapsed-duration guarantees and pause allocator transitions during an unresolved clock incident if the required bound cannot be met.
PostgreSQL documents that SKIP LOCKED omits locked rows and can support queue-like consumers. It does not establish strict FIFO. Use it to distribute independent class jobs if appropriate; skipping a locked earlier customer within a class would violate this baseline policy. Keep the class serialization boundary authoritative even when a worker job is delivered twice.
7. Follow-ups and self-review
Follow-ups: Should a group accept fewer seats? Only through an explicit change to the indivisible-group contract. Can payment occur while holding the class lock? Avoid external calls inside the transaction; introduce a bounded checkout hold and separately resolve late payment outcomes. Should an expired offer regain its old position? That changes fairness and needs an explicit rule. Can one user join through several accounts? Identity and abuse controls are separate from FIFO ordering.
Score 0–2 each for fairness policy, transactional capacity accounting, deadline race handling, notification recovery and holding-cost calculations. A strong answer demonstrates both no overbooking and a truthful user-visible ordering promise. Test the three-seat/four-person head-of-line example before calling the system fair.
Sources and next steps
- PostgreSQL SELECT documentation — row-locking and SKIP LOCKED behavior.
The queue policy, state machine, race analysis and numerical examples are original. Compare initial inventory reservations and durable delayed messages.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.