Paginate forward and backward through results
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of pagination mechanics, cursor/token design, array indexing, and state management for bidirectional (forward and backward) navigation.
Constraints
- page_size > 0
- items are already sorted in deterministic order
Examples
Input: (['a', 'b', 'c', 'd', 'e'], 2, [['first'], ['next', 2], ['next', 4], ['next', 5], ['prev', 5], ['prev', 1], ['prev', 0]])
Expected Output: [[['a', 'b'], 2], [['c', 'd'], 4], [['e'], 5], [[], 5], [['d', 'e'], 3], [['a'], 0], [[], 0]]
Input: ([1, 2, 3], 5, [['first'], ['next', 3], ['prev', 3]])
Expected Output: [[[1, 2, 3], 3], [[], 3], [[1, 2, 3], 0]]
Input: ([], 3, [['first'], ['next', 0], ['prev', 0]])
Expected Output: [[[], 0], [[], 0], [[], 0]]
Hints
- Use the cursor as an array boundary.
- Clamp invalid cursors into [0, len(items)].