Implement Stable Cursor Pagination for a Rider's Trip History
Last updated: November 29, 2026
Quick Overview
Implement the API that returns a rider's past trips newest first, 20 at a time, such that trips completed while the rider is scrolling do not cause duplicates or skipped rows on later pages.
Uber
November 29, 20260
0
0 solved
Implement the API that returns a rider's past trips newest first, 20 at a time, such that trips completed while the rider is scrolling do not cause duplicates or skipped rows on later pages.
Trip history is a basic Uber API, and offset pagination on a list that grows at the top is a classic bug. Interviewers check keyset pagination with a unique tiebreaker, opaque cursors, and the SQL index that makes it fast.
What the Interviewer Expects
- Explain why OFFSET pagination breaks when new rows are inserted
- Use keyset pagination on (completed_at, id) with id as tiebreaker
- Return an opaque, URL-safe cursor
- Fetch limit plus one row to detect whether more pages exist
- Name the composite index that supports the query
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
- How would you support filtering by product type without breaking cursors?
- What if a trip's completed_at is corrected after the user loaded page one?
- How do you version the cursor format safely?
- Why not just use the trip id alone as the cursor?
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
GET /riders/{id}/trips?limit=20&cursor=... returns {trips, next_cursor}. Order is newest first. New trips appearing mid-scroll must not shift later pages. The cursor is opaque so the server can ch...
High-Level Design
With OFFSET 20, a new trip at the top pushes every row down one, so page two repeats the last row of page one. Keyset pagination instead says "give me rows strictly older than the last one I saw". T...