All Challenges
3 PointsIntermediateMongoDB
00:00
MongoDB: Keyset Cursor-Based Pagination & Sorting Engine
Implement high-performance cursor pagination with base64 token encoding, avoiding offset skip() performance degradation on large collections.
### Problem Overview
In large-scale MongoDB deployments with millions of records, classic offset pagination using skip() degrades drastically. You must build a keyset pagination engine that serializes opaque Base64 cursor tokens containing composite sort values (e.g. [createdAt, _id]), executes efficient range queries, and returns Relay-compliant page info.
### Functional Requirements
1. **`paginate(options: PaginationOptions): PaginationResult<T>`**:
- `limit`, `after`, `before`, `sortField`, `sortOrder`.
2. **Deterministic Tie-Breaking**:
- Always append `_id` as the secondary tie-breaker.
3. **Cursor Serialization**:
- Encode cursor token as URL-safe Base64 JSON.
4. **Relay-Compliant PageInfo Output**:
- Returns `{ edges: Array<{ cursor: string, node: T }>, pageInfo: { hasNextPage, hasPreviousPage, startCursor, endCursor } }`.
AI Review Rubric Dimensions (1–10 Points):
- Cursor serialization
- Tie-breaking logic
- PageInfo accuracy
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
34 lines•748 characters•
AI Review Ready