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:
-
score
ascending
-
id
ascending (tie-breaker)
-
Pagination must be
cursor-based
(not offset-based):
-
The client provides a
cursor
representing “the last row seen”.
-
The server returns up to
pageSize
rows
after
that cursor, plus a
nextCursor
.
-
If there are no more rows,
nextCursor = null
.
Function signature (conceptual)
-
Input:
minScore
,
pageSize
,
cursor
(nullable)
-
Output:
(rows[], nextCursor)
Follow-ups
-
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?
Constraints
-
Up to 1,000,000 rows.
-
pageSize
up to 1,000.
-
Assume no concurrent writes during a single paginated scan (you can state this assumption explicitly).