Paginate forward and backward through results
CoinbaseSoftware Engineer · Technical ScreenLast 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
itemssorted in a fixed, deterministic order. - An integer
pageSize > 0.
Design a paginator with the following operations:
getFirstPage()→ returns the firstpageSizeitems and a cursor representing the position after the last returned item.getNextPage(cursor)→ given a cursor returned by a previous call, return the nextpageSizeitems and a new cursor. If there are no more items, return an empty list and a special "end" cursor.getPrevPage(cursor)→ given a cursor, return the previous page ofpageSizeitems 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 ofpageSize.
Implement the data structure and its methods with clear handling of indices and cursors.
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.