KatInterview

Bidirectional Page Navigation with Integer Cursors

Data Structure DesignAuto-graded · 14 test cases
EasyAuto-gradedData Structure Designdesignpaginationsimulation

Last 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. Let n = len(items).
  • page_size: a positive integer k.
  • commands: a list of strings. Each is one of "getFirstPage", "getNextPage" or "getPrevPage".
  • params: a list with the same length as commands. params[i] is the argument list for commands[i]. It is empty for "getFirstPage" and contains exactly one integer cursor c for 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: let e = min(k, n). Return [items[0:e], e].
  • getNextPage with cursor c:
    • If c >= n, return [[], n].
    • Otherwise let e = min(c + k, n) and return [items[c:e], e].
  • getPrevPage with cursor c:
    • If c <= 0, return [[], 0].
    • Otherwise let s = max(0, c - k) and return [items[s:c], s].

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^5
  • 1 <= page_size <= 10^5
  • len(commands) == len(params) <= 10^4
  • Every cursor passed to getNextPage or getPrevPage satisfies 0 <= 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.