Interview trap 01
The double-booked seat
Two users hit 'Book' on the last seat at the same moment. Your code checks availability first. Who gets the seat?
Recorded from a real run against Postgres. Timings are real.
Alice and Bob hit the button at the same moment.
Press play, or step with ← → . Click any event on the timeline to jump to it.
What users were told
seats · 1 row
| id | status | booked_by |
|---|---|---|
| A1 | available | NULL |
bookings · 0 rows
| (empty) |
Why it breaks
Both requests read 'available' before either one writes. The check and the write are separate statements, so the gap between them is a window anyone can walk through.
The interview answer
Never check-then-act across two statements. Make the check part of the write (UPDATE … WHERE status = 'available'), and back it with a UNIQUE constraint.
Make it fail on your machine
The proof fires 50 concurrent requests at a real Postgres. One command.
npm run 01:bug