Katkat

Paginate forward and backward through results

CoinbaseSoftware Engineer · Technical Screen

Last updated

This question evaluates understanding of pagination mechanics, cursor/token design, array indexing, and state management for bidirectional (forward and backward) navigation.

You are asked to extend a basic pagination system to support forward and backward navigation, similar to a cursor-based API.

You are given:

  • A zero-indexed array of items items sorted in a fixed, deterministic order.
  • An integer pageSize > 0.

Design a paginator with the following operations:

  1. getFirstPage() → returns the first pageSize items and a cursor representing the position after the last returned item.
  2. getNextPage(cursor) → given a cursor returned by a previous call, return the next pageSize items and a new cursor. If there are no more items, return an empty list and a special "end" cursor.
  3. getPrevPage(cursor) → given a cursor, return the previous page of pageSize items and a new cursor pointing to the start of that page. If there is no previous page, return an empty list and a "start" cursor.

Requirements:

  • Define what the cursor contains (it could just be an index or a more complex token).
  • All operations should run in O(pageSize) time.
  • Handle edge cases correctly, including:
  • Moving past the last page with getNextPage.
  • Moving before the first page with getPrevPage.
  • Cases where len(items) is not a multiple of pageSize.

Implement the data structure and its methods with clear handling of indices and cursors.