Book Seats on a Shared Ride Without Overselling Under Concurrency
Last updated: November 29, 2026
Quick Overview
UberX Share rides have a fixed number of open seats. Implement seat booking against a SQL database so that concurrent riders can never push seats below zero, a rider cannot book the same ride twice, and the seat count and booking rows always agree.
Uber
November 29, 20260
0
0 solved
UberX Share rides have a fixed number of open seats. Implement seat booking against a SQL database so that concurrent riders can never push seats below zero, a rider cannot book the same ride twice, and the seat count and booking rows always agree.
Shared rides at Uber create contention for the last seat during busy periods, and read-then-write code in the application layer oversells under load. The interviewer checks transaction boundaries, the lost update anomaly, conditional updates, and constraints as a second line of defense.
What the Interviewer Expects
- Describe the lost update when reading seats then writing seats minus one
- Use a single conditional UPDATE and check the affected row count
- Put the seat decrement and booking insert in one transaction
- Add constraints (CHECK seats_left >= 0, unique rider per ride) as safety nets
- Discuss isolation levels and row locks versus optimistic versioning
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Would SELECT ... FOR UPDATE work here? What does it cost?
- How would you implement cancellation and seat release correctly?
- How does this change if seats live in a cache in front of the database?
- What isolation level does the conditional update need in Postgres or MySQL?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
book_seats(ride, rider, n) succeeds only if at least n seats remain and the rider has not already booked; on success seats drop by n and a booking row exists, atomically. Many riders may race for th...
High-Level Design
The broken version: SELECT seats_left (sees 1), then UPDATE ... SET seats_left = 0. Two transactions both read 1 and both write 0, so two riders book one seat. The fix moves the check into the wri...