API Pagination Collector
The customer’s API returns 100 rows at a time behind a cursor. You need all of them. Follow the "next" pointer page by page until it runs out — the loop behind every "sync everything" integration. Solve it in Python or TypeScript, with hidden tests.
The problem
Implement collect(pages). pages models a cursor-paginated API as a list where each entry is {"items": [...], "next": index-or-None}. Start at index 0, append its items, follow next to the next page, and repeat until next is None. Return the flat list of all items in page order. (Guard against a cycle: never visit the same page twice.)
pages = [{"items":[1,2],"next":1},{"items":[3],"next":2},{"items":[4,5],"next":null}][1, 2, 3, 4, 5]- Begin at index 0 and follow
nextuntil it isNone/null. - Concatenate each page’s
itemsin the order visited. - Stop if you revisit a page (a malformed API could loop) — do not spin forever.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement collect(pages): walk from index 0 following next, extending an output list with each page’s items, tracking visited indices to break any cycle.
- Keep a current index (start 0) and a
seenset of visited indices. - While the index is not None and not already seen: mark it seen, extend the output with
pages[i]["items"], set i topages[i]["next"]. - Return the accumulated items.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.