KatInterview

Resumable Batch Reader for a Linked Page Feed

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designdesignpaginationbuffering

Last updated

Simulate a stateful reader that serves batches of N items in order from a chained page feed, buffering surplus items and handling empty pages, loops and the end of the feed.

Resumable Batch Reader for a Linked Page Feed

A remote feed publishes its items in pages. Each page is stored under a string token and holds two fields:

  • items: a list of values, in feed order.
  • nextPage: the token of the following page, or null when this page is the last one.

You are given a lookup table page_map that stands in for the remote feed: it maps each token to its page. You must simulate a stateful reader that starts at start_token and hands out items in batches. State carries over between calls: each request continues exactly where the previous one stopped.

Operations

You receive two parallel lists, operations and arguments. The i-th entry of operations is an operation name, and the i-th entry of arguments is its argument list. The only operation is "fetchN", whose argument list is [n].

Process the operations in order against one reader that lives for the whole run. Return a list containing one result per "fetchN" operation, in order. Each result is itself a list of items. Operations with any other name are ignored and add nothing to the output.

Behavior of fetchN(n)

  • If n <= 0, return [] and do not touch the feed.
  • Otherwise return the next n not-yet-returned items in feed order. If fewer than n remain in the whole feed, return all of them.
  • Items already downloaded but not yet returned must be served before any new page is downloaded. Do not download more pages than needed to satisfy the request. Stop as soon as at least n items are available.
  • When a downloaded page holds more items than the current request needs, the surplus must be kept for later calls. Nothing may be dropped or repeated, and order must be preserved across pages.
  • A page with an empty items list is still a normal page. If its nextPage is non-null, keep going to the next page.
  • The feed has ended when a downloaded page has no nextPage (missing or null), or when its nextPage is the literal string "null". After that, no further downloads happen, but any buffered items can still be returned by later calls. Once the buffer is empty as well, calls return [].
  • If a page lacks an items field, treat it as having no items.
  • If the token to download is not a key of page_map (including a null start token), treat it as an empty page that ends the feed.
  • Loop protection: the reader remembers every token it has downloaded. If it needs more items and the next token to download was already downloaded earlier, it treats the feed as ended without downloading again. Items already buffered stay available.

Constraints

  • 0 <= len(operations) == len(arguments) <= 10^4
  • Each "fetchN" argument list holds exactly one integer n, with -10^4 <= n <= 10^4.
  • The total number of items across all pages is at most 10^5.
  • Tokens are strings (the start token may also be null). Items can be any JSON values and are returned unchanged.
  • Calls are strictly sequential, so there is no concurrency to handle.

Examples

Example 1

Input: run_paged_reader('p1', {'p1': {'items': [1, 2], 'nextPage': 'p2'}, 'p2': {'items': [3, 4], 'nextPage': None}}, ['fetchN', 'fetchN', 'fetchN'], [[3], [2], [0]])

Output: [[1, 2, 3], [4], []]

Example 2

Input: run_paged_reader('a', {'a': {'items': [], 'nextPage': 'b'}, 'b': {'items': [7], 'nextPage': None}}, ['fetchN', 'fetchN'], [[1], [2]])

Output: [[7], []]

Hints

Hint 1

The reader needs three things that survive between calls: the next token to download, a buffer of downloaded-but-unreturned items, and a flag saying the feed has ended.

Hint 2

Serve from the buffer first. Download pages in a loop only while the buffer holds fewer than n items and the feed has not ended. Then slice off the first n items and keep the rest.

Hint 3

Keep a set of tokens already downloaded to detect cycles, and check it before each download. Also remember that an empty page with a next token is not the end, but a missing, null or "null" next token is.