Bidirectional Page Navigation with Integer Cursors
Data Structure DesignAuto-graded · 14 test casesLast updated
Simulate a paginator that serves pages forward and backward from integer cursors, with correct clamping at both ends and for a short final page.
Page Navigation Simulator
A list of results is served in fixed-size pages. The client keeps an integer cursor, which is an index into the list, and asks for the page that comes after or before it. You are given the list, the page size, and a sequence of commands to run in order. Return the result of each command.
Inputs
items: a list of integers, in a fixed order. Letn = len(items).page_size: a positive integerk.commands: a list of strings. Each is one of"getFirstPage","getNextPage"or"getPrevPage".params: a list with the same length ascommands.params[i]is the argument list forcommands[i]. It is empty for"getFirstPage"and contains exactly one integer cursorcfor the other two commands.
Commands are independent. Each one uses only the cursor given in its own argument list, so the paginator keeps no state between commands.
Behavior of each command
Every command produces a pair [page, new_cursor], where page is a list of items and new_cursor is an integer.
getFirstPage: lete = min(k, n). Return[items[0:e], e].getNextPagewith cursorc:- If
c >= n, return[[], n]. - Otherwise let
e = min(c + k, n)and return[items[c:e], e].
- If
getPrevPagewith cursorc:- If
c <= 0, return[[], 0]. - Otherwise let
s = max(0, c - k)and return[items[s:c], s].
- If
The previous page is the block of up to k items that ends right before c. It is not snapped to multiples of k. For example, with 7 items and k = 3, going back from cursor 7 gives the items at indices 4 to 6, and the new cursor is 4.
Return a list with one [page, new_cursor] entry per command, in the same order as commands.
Constraints
0 <= n <= 10^51 <= page_size <= 10^5len(commands) == len(params) <= 10^4- Every cursor passed to
getNextPageorgetPrevPagesatisfies0 <= c <= n. - Command names are always one of the three listed above.
- Each command should take O(
page_size) time.
Examples
Example 1
Input: run_page_queries([1, 2, 3, 4, 5, 6, 7], 3, ['getFirstPage', 'getNextPage', 'getNextPage', 'getNextPage', 'getPrevPage', 'getPrevPage'], [[], [3], [6], [7], [7], [4]])
Output: [[[1, 2, 3], 3], [[4, 5, 6], 6], [[7], 7], [[], 7], [[5, 6, 7], 4], [[2, 3, 4], 1]]
Example 2
Input: run_page_queries([10, 20, 30, 40], 2, ['getFirstPage', 'getNextPage', 'getPrevPage', 'getPrevPage'], [[], [2], [4], [2]])
Output: [[[10, 20], 2], [[30, 40], 4], [[30, 40], 2], [[10, 20], 0]]
Hints
Hint 1
Treat each command independently. The cursor is just an index, so every answer comes from a slice of items plus a clamped boundary.
Hint 2
For next, the page starts at the cursor and ends at min(cursor + k, n). For prev, the page ends at the cursor and starts at max(0, cursor - k). The new cursor is the far edge of the slice you returned.
Hint 3
Handle the boundaries first: next with cursor >= n returns an empty page and cursor n, and prev with cursor <= 0 returns an empty page and cursor 0. Also check that a short final page and empty items behave correctly.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The cursor is a plain integer index, and each command is a pure function of that index. There is no state to maintain between commands, so we loop over the commands and answer each one with a slice.
- First page: the slice
items[0:e], wheree = min(k, n). The new cursor ise, the position just after the last returned item. - Next page from
c: ifc >= n, nothing is left, so return an empty page and cursorn. Otherwise returnitems[c:e]withe = min(c + k, n). Theminmakes a short final page work. The new cursor ise. - Previous page from
c: ifc <= 0, nothing precedes it, so return an empty page and cursor0. Otherwise returnitems[s:c]withs = max(0, c - k). Themaxhandles a first page shorter thank. The new cursor iss, the start of the page just returned.
Why it is correct
The next page begins exactly where the cursor points, and the previous page ends exactly where the cursor points. So the two directions are consistent: a next page returns cursor e, and a prev call from e returns a page that ends at e. Clamping with min and max keeps every slice inside [0, n]. The two boundary checks produce the required empty pages with the cursors n and 0. Empty items works too, since n = 0 makes first and next return [[], 0].
Complexity
- Time: O(
page_size) per command, because each command builds one slice of at mostpage_sizeitems. The total is O(len(commands) * page_size). - Space: O(
page_size) per returned page. The input list is never copied.
Reference solution
from typing import List
def run_page_queries(items: List[int], page_size: int, commands: List[str], params: List[List[int]]) -> List[List[object]]:
n = len(items)
results = []
for command, args in zip(commands, params):
if command == "getFirstPage":
end = min(page_size, n)
results.append([items[:end], end])
elif command == "getNextPage":
cursor = args[0]
if cursor >= n:
results.append([[], n])
else:
end = min(cursor + page_size, n)
results.append([items[cursor:end], end])
elif command == "getPrevPage":
cursor = args[0]
if cursor <= 0:
results.append([[], 0])
else:
start = max(0, cursor - page_size)
results.append([items[start:cursor], start])
else:
raise ValueError("Unknown command")
return results