L2 · Working engineerAdvanced L2~6 min · 4 tests#95

Fetch All Pages From a Paginated API

Collect every item from a paginated API by following the next-page cursor until it ends, without assuming the page count. Generator version included.

The problem

fetch_page(page) (provided by the test) returns {"items": [...], "next": <next page number or None>}. Start at page 1, follow "next" until it is None, and return all items in order.

Do not assume how many pages there are.

Examples

  1. Example 1

    Input

    fetch_all_pages(fake_api([[1, 2, 3], [4, 5, 6], [7, 8]]))

    Expected output

    [1, 2, 3, 4, 5, 6, 7, 8]
  2. Example 2

    Input

    call_count(fetch_all_pages, [[1], [2], [3], [4]])

    Expected output

    4

+ 2 hidden tests on Submit — single empty page, empty page in the middle.

Edge cases to ask about

  • Single page
  • Empty page in the middle
How the tests call your code

These helpers run before your code. The test inputs above call them.

def fake_api(pages):
    calls = []
    def fetch_page(page):
        calls.append(page)
        items = pages[page - 1]
        return {"items": items, "next": page + 1 if page < len(pages) else None}
    fetch_page.calls = calls
    return fetch_page

def call_count(fn, pages):
    api = fake_api(pages)
    fn(api)
    return len(api.calls)

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · fetch_all_pages
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Loop on the cursorO(p + n)O(n)p requests, n items.
    bestGenerator that yields itemsO(p + n)O(page size)Callers can stop early or stream into a database.
    Walkthrough of the optimal approach (try it yourself first)

    Loop on the cursor: fetch, extend, move to response["next"], stop at None. Never infer the end from an empty page — the hidden test has an empty page in the middle.

    In production: turn it into a generator so callers can stream, add retries with backoff for transient failures (question 84), respect rate limits (85), and prefer opaque cursors over page numbers so inserted rows do not shift pages.

    Complexity: O(p + n) time, O(n) space. One request per page (p) and each of the n items is copied once into the result.

    Reveal the reference solution
    def fetch_all_pages(fetch_page):
        items = []
        page = 1
        while page is not None:
            response = fetch_page(page)
            items.extend(response["items"])
            page = response["next"]
        return items

    Follow-ups interviewers ask

    • Make it a generator.
    • Fetch pages concurrently when the total page count is known.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Fetch All Pages From a Paginated API in Python?

    The optimal solution runs in O(p + n) time and O(n) auxiliary space. One request per page (p) and each of the n items is copied once into the result.

    What is the brute-force approach, and how do you optimise it?

    Loop on the cursor: O(p + n) time, O(n) space. p requests, n items. Generator that yields items: O(p + n) time, O(page size) space. Callers can stop early or stream into a database.

    What follow-up questions do interviewers ask about Fetch All Pages From a Paginated API?

    Make it a generator. Fetch pages concurrently when the total page count is known.