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:
-
getFirstPage()
→ returns the first
pageSize
items and a
cursor
representing the position after the last returned item.
-
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.
-
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.