Implement cursor-based query pagination
CoinbaseSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of cursor-based pagination, deterministic ordering with tie-breakers, cursor encoding, and in-memory data-structure management for efficient filtered queries.
You are building a small in-memory database that stores rows with the following fields:
id(string, unique)score(integer)payload(string)
You need to support a read API that returns results in a deterministic order and supports pagination.
Requirements
- Store rows in memory (you may assume a single process).
- Implement a query that returns all rows with
score >= minScore. - Results must be sorted by:
scoreascendingidascending (tie-breaker)
- Pagination must be cursor-based (not offset-based):
- The client provides a
cursorrepresenting “the last row seen”. - The server returns up to
pageSizerows after that cursor, plus anextCursor. - If there are no more rows,
nextCursor = null.
Function signature (conceptual)
- Input:
minScore,pageSize,cursor(nullable) - Output:
(rows[], nextCursor)
Constraints
- Up to 1,000,000 rows.
pageSizeup to 1,000.- Assume no concurrent writes during a single paginated scan (you can state this assumption explicitly).
Follow-up questions
- How do you encode the cursor so it is stable and unambiguous?
- How do you avoid duplicates or missing rows across pages when there are many rows with the same `score`?
- What is the time complexity of your approach, and how would you improve it for large datasets?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.