KatInterview

Newest-First Transaction Pager with Offset and Cursor Modes

Heaps, Sorting & SearchAuto-graded · 21 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortingpaginationkeyset-cursor

Last updated

Build a pager over transaction records that sorts newest-first with a deterministic tie-break and supports offset and keyset-cursor navigation with correct metadata.

You are building the read side of a transaction history endpoint. You get an unordered list of transaction records and must return one page of them, newest first. The caller picks one of two paging styles.

Each record is a dictionary with the keys id (unique, comparable with the other ids), userId, amount and createdAt (a numeric timestamp).

Function

Implement a function taking these parameters, in this order:

  • records: the list of transaction dictionaries, in arbitrary order.
  • mode: either "offset" or "cursor".
  • page_size: the maximum number of records to return on the page.
  • position: where the page starts. Its meaning depends on mode.

It returns a dictionary.

Ordering

Records are ordered by the pair (createdAt, id) in descending order. The record with the largest createdAt comes first. Records with equal createdAt are ordered by larger id first. Because ids are unique, this is a strict total order, and every record has exactly one position.

Offset mode (mode == "offset")

  • position is a non-negative integer offset into the ordered list, or None, which means offset 0.
  • data is the ordered records from index offset up to (not including) offset + page_size. Fewer records are returned if the list runs out. If the offset is at or beyond the end, data is empty.
  • Return a dictionary with exactly these keys:
    • "data": the page, as a list of the original record dictionaries.
    • "hasNext": True if at least one record exists after the last record of this page, that is, offset + len(data) < total. Otherwise False.
    • "nextCursor": always None in this mode.
    • "total": the number of records in records.

Cursor mode (mode == "cursor")

  • position is either None (start from the newest record) or a two-element list [createdAt, id]. This is the sort key of the last record the caller has already seen.
  • The eligible records are those whose key (createdAt, id) is strictly smaller than the cursor pair under ordinary tuple comparison, kept in the ordering above. If position is None, all records are eligible. The cursor does not need to match any existing record.
  • data is the first page_size eligible records.
  • Return a dictionary with exactly these keys (there is no "total" key in this mode):
    • "data": the page.
    • "hasNext": True if some eligible records did not fit on the page, otherwise False.
    • "nextCursor": if hasNext is True and the page is non-empty, the list [createdAt, id] of the last record on the page. In every other case it is None.

Constraints

  • 0 <= len(records) <= 10^5
  • page_size >= 0. With page_size == 0 the page is empty. In cursor mode hasNext is still True if any record is eligible, and nextCursor is None because the page has no last record.
  • id values are unique within records and mutually comparable (all integers or all strings, matching the cursor's id type).
  • createdAt values are comparable numbers and may repeat.
  • In offset mode, position is None or an integer >= 0.
  • In cursor mode, position is None or a list [createdAt, id].
  • Do not modify the input records. Return the original record dictionaries unchanged.

Examples

Example 1

Input: fetch_transaction_page([{'id': 1, 'userId': 10, 'amount': 12.5, 'createdAt': 1000}, {'id': 2, 'userId': 11, 'amount': 8, 'createdAt': 1002}, {'id': 3, 'userId': 10, 'amount': 20, 'createdAt': 1002}, {'id': 4, 'userId': 12, 'amount': 5, 'createdAt': 999}], 'offset', 2, 0)

Output: {'data': [{'id': 3, 'userId': 10, 'amount': 20, 'createdAt': 1002}, {'id': 2, 'userId': 11, 'amount': 8, 'createdAt': 1002}], 'hasNext': True, 'nextCursor': None, 'total': 4}

Example 2

Input: fetch_transaction_page([{'id': 1, 'userId': 10, 'amount': 12.5, 'createdAt': 1000}, {'id': 2, 'userId': 11, 'amount': 8, 'createdAt': 1002}, {'id': 3, 'userId': 10, 'amount': 20, 'createdAt': 1002}, {'id': 4, 'userId': 12, 'amount': 5, 'createdAt': 999}], 'cursor', 2, None)

Output: {'data': [{'id': 3, 'userId': 10, 'amount': 20, 'createdAt': 1002}, {'id': 2, 'userId': 11, 'amount': 8, 'createdAt': 1002}], 'hasNext': True, 'nextCursor': [1002, 2]}

Example 3

Input: fetch_transaction_page([{'id': 1, 'userId': 10, 'amount': 12.5, 'createdAt': 1000}, {'id': 2, 'userId': 11, 'amount': 8, 'createdAt': 1002}, {'id': 3, 'userId': 10, 'amount': 20, 'createdAt': 1002}, {'id': 4, 'userId': 12, 'amount': 5, 'createdAt': 999}], 'cursor', 2, [1002, 2])

Output: {'data': [{'id': 1, 'userId': 10, 'amount': 12.5, 'createdAt': 1000}, {'id': 4, 'userId': 12, 'amount': 5, 'createdAt': 999}], 'hasNext': False, 'nextCursor': None}

Hints

Hint 1

Sorting by timestamp alone leaves ties undefined. Sort by the pair (createdAt, id), both descending, so each record has one fixed position.

Hint 2

Offset mode is a slice of the sorted list. hasNext is just whether the index right after the slice is still inside the list.

Hint 3

In cursor mode, keep records whose (createdAt, id) tuple is strictly less than the cursor tuple. Compare keys, do not look the cursor record up. Compare the eligible count with the page length to decide hasNext, and only emit a nextCursor when hasNext is true and the page is not empty.