Flatten a Cursor-Paginated Orders API Into One Lazy Iterator

Last updated: November 29, 2026

Quick Overview

An internal orders API returns pages of results plus a next cursor (None when done), and pages may be empty. Build an iterator that yields orders one at a time across pages, fetching each page only when it is needed.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


An internal orders API returns pages of results plus a next cursor (None when done), and pages may be empty. Build an iterator that yields orders one at a time across pages, fetching each page only when it is needed.

Amazon services expose paginated APIs everywhere, and client code that eagerly loads every page causes memory spikes and throttling. Interviewers check laziness, correct handling of empty pages and the final cursor, and how you would add retries.

What the Interviewer Expects
  • Fetch the first page lazily, not in the constructor
  • Advance across empty pages without yielding anything
  • Stop when the cursor is None and the buffer is empty
  • Keep has_next idempotent (no double fetches)
  • Discuss retries with backoff on throttling
Key Topics to Cover
iterators
pagination
lazy loading
API clients
error handling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you prefetch the next page in the background to hide latency?
  • How would you resume iteration after a crash, given a saved cursor?
  • What if the API can return the same order on two pages during concurrent writes?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a function fetch(cursor) -> (items, next_cursor); the first call uses cursor None. Output: an iterator with has_next() and next(). Pages can be empty even when more pages follow. Only the...

High-Level Design

Keep a buffer (the current page's items), an index into it, the next cursor and a done flag. A private _fill() method loops while the buffer is exhausted and the stream is not done: it fetches the...


Submit Your Answer
Markdown supported