Cursor-Based Pagination
Asked of: Software Engineer
Last updated

What's being tested
Cursor-based pagination tests whether you can return stable slices from an ordered result set without relying on fragile OFFSET semantics. Interviewers probe deterministic ordering, cursor/token design, bidirectional navigation, filtering, duplicate avoidance, and complexity tradeoffs for both in-memory and database-backed queries.
Patterns & templates
-
Stable sort key: order by a business key plus unique tie-breaker, e.g.
ORDER BY created_at DESC, id DESC, to avoid skipped or duplicated rows. -
Forward cursor predicate: for descending order, fetch after
(ts, id)using(created_at, id) < (:ts, :id); invert comparisons for ascending order. -
Backward pagination: reverse the inequality, fetch
limit + 1, then reverse results before returning so UI order stays consistent. -
Opaque cursor token: encode
{created_at, id, filters, direction}withbase64or signed JSON; never expose mutable array indexes as durable cursors. -
Limit-plus-one: request
limit + 1rows to computehas_next_pageorhas_previous_pagewithout an extra count query. -
Filter composition: apply conjunctive filters before pagination; index should match
WHEREfilters thenORDER BY, e.g.(user_id, status, created_at, id). -
Complexity target: database seek pagination should be
O(limit)after index seek; in-memory versions are oftenO(n log n)sort plusO(limit)slice.
Common pitfalls
Pitfall: Using
OFFSETandLIMITas the primary solution; inserts or deletes between requests can shift rows and cause duplicates or missing items.
Pitfall: Sorting only by
created_at; equal timestamps make ordering nondeterministic unless you add a unique tie-breaker likeid.
Pitfall: Treating backward pagination as “same query with previous cursor”; you must flip comparison direction, fetch extra, and restore display order.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Debug and Extend Cursor QueriesCoinbase · Software Engineer · Onsite · hard
- Implement cursor-based query paginationCoinbase · Software Engineer · Technical Screen · medium
- Paginate forward and backward through resultsCoinbase · Software Engineer · Technical Screen · medium
- Implement basic pagination for a listCoinbase · Software Engineer · Technical Screen · medium
- Implement filters and cursor paginationCoinbase · Software Engineer · Onsite · Medium
- Design a reusable React comment componentCoinbase · Software Engineer · Onsite · hard
Related concepts
- Web Crawlers, URL Normalization, And PolitenessSystem Design
- Snapshotable Collections And IteratorsCoding & Algorithms
- Message Splitting With Paginated SuffixesCoding & Algorithms
- Nested Iterators And Lazy Stack TraversalCoding & Algorithms
- Search, Autocomplete And Restaurant DiscoverySystem Design
- Binary Search And Feasibility OptimizationCoding & Algorithms