KatInterview

Resume a Sorted Score Listing from a Last-Seen Key

Heaps, Sorting & SearchAuto-graded · 21 test cases
MediumAuto-gradedHeaps, Sorting & Searchpaginationsortingtuple-comparison

Last updated

Filter records by a minimum score, order them by score then id, and return one page after a (score, id) cursor along with the cursor for the following page.

You are given a collection of records, where each record is a dictionary with three keys:

  • id: a string, unique across the collection
  • score: an integer
  • payload: a string

Write a function that returns one page of results from this collection, using a key-based cursor rather than a numeric offset.

Task

  1. Keep only records whose score is greater than or equal to min_score.
  2. Order the kept records by score ascending. Records with equal scores are ordered by id ascending (plain string comparison).
  3. If cursor is not None, it is a two-element list [score, id] describing the last record the caller has already seen. Discard every record whose (score, id) pair is less than or equal to the cursor pair, so only records strictly after it remain. The cursor pair does not need to match an existing record.
  4. Take the first page_size records of what remains. These form the page.
  5. Compute the next cursor:
    • If there are more records beyond the page, it is [score, id] of the last record in the page.
    • Otherwise it is None. In particular, if the remaining records fit exactly in the page, the next cursor is None.

Return value

Return a two-element list [page, next_cursor], where page is the list of record dictionaries (unchanged, with all three keys) in the order described above, and next_cursor is either [score, id] or None.

If nothing qualifies, return [[], None].

Constraints

  • Up to 1,000,000 records.
  • 1 <= page_size <= 1000.
  • id values are unique; score values are integers and may be negative or repeated.
  • The input list is in arbitrary order and does not change during a paginated scan.
  • Cursor comparison uses the pair (score, id): compare scores numerically first, then ids as strings.

Examples

Example 1

Input: fetch_page_after_key([{'id': 'b', 'score': 2, 'payload': 'B'}, {'id': 'a', 'score': 1, 'payload': 'A'}, {'id': 'c', 'score': 2, 'payload': 'C'}], 1, 2, None)

Output: [[{'id': 'a', 'score': 1, 'payload': 'A'}, {'id': 'b', 'score': 2, 'payload': 'B'}], [2, 'b']]

Example 2

Input: fetch_page_after_key([{'id': 'z', 'score': 4, 'payload': 'Z'}, {'id': 'a', 'score': 4, 'payload': 'A'}, {'id': 'm', 'score': 3, 'payload': 'M'}], 4, 1, [4, 'a'])

Output: [[{'id': 'z', 'score': 4, 'payload': 'Z'}], None]

Example 3

Input: fetch_page_after_key([{'id': 'x', 'score': 1, 'payload': 'X'}], 2, 5, None)

Output: [[], None]

Hints

Hint 1

Because ids are unique, the pair (score, id) gives every record a distinct position in a strict total order, so a cursor is just a position in that order.

Hint 2

You do not need to sort everything. Filter by min_score and by 'strictly greater than the cursor pair', then pick only the smallest few items.

Hint 3

Select page_size + 1 candidates: if the extra one exists, there is more data and the next cursor is the last item of the page; otherwise the next cursor is None.