Paginated Fetch-All
easy · cursor traversal
pages is a fake API mapping string cursor to {'items': list of integers, 'next': string cursor or None}. Starting at start, append page items in order, including duplicates. Stop at a None next cursor, a missing cursor, or before fetching a cursor a second time. Return {'items': collected values, 'visited': cursors in request order, 'error': None, 'missing_cursor', or 'cursor_cycle'}. A missing cursor is not added to visited because no page was fetched. start may be None (empty successful traversal). No HTTP requests.
Examples (JSON; null means Python None):
Input: {"pages": {"a": {"items": [1, 2], "next": "b"}, "b": {"items": [2, 3], "next": null}}, "start": "a"}
Output: {"items": [1, 2, 2, 3], "visited": ["a", "b"], "error": null}
Input: {"pages": {"a": {"items": [1], "next": "a"}}, "start": "a"}
Output: {"items": [1], "visited": ["a"], "error": "cursor_cycle"}
Starter code
def fetch_all(pages: dict, start: str | None) -> dict:
"""Follow fake API cursors once each and report broken chains."""
pass
Loading editor…
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler