Lazy Iterator Over a Paginated Transactions API With Peek

Last updated: November 29, 2026

Quick Overview

A transactions API returns pages as (items, next_token), with next_token None on the last page and some pages possibly empty. Build an iterator that yields transactions one at a time, fetches pages only when needed, skips empty pages, and supports peek().

Capital One
Coding & Algorithms
Software Engineer
Capital One
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A transactions API returns pages as (items, next_token), with next_token None on the last page and some pages possibly empty. Build an iterator that yields transactions one at a time, fetches pages only when needed, skips empty pages, and supports peek().

Capital One exposes paginated APIs (including its DevExchange developer APIs), and client SDK code that hides pagination is a common real task. The phone screen checks that you implement the Python iterator protocol correctly, fetch lazily, and do not lose an item when peek and next interleave.

What the Interviewer Expects
  • Implement __iter__ and __next__ correctly
  • Fetch the next page only when the current page is exhausted
  • Skip empty pages in a loop, not a single if
  • Raise StopIteration exactly once the last page is exhausted
  • Make peek idempotent and consistent with next
Key Topics to Cover
Iterators
Pagination
Lazy evaluation
API client design
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 add retry with backoff when a page fetch fails?
  • How would you prefetch the next page in the background to hide latency?
  • How would you resume iteration after a crash, given only the last token?
  • How would you write this as a generator function, and what is lost?
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
  • fetch_page(token) returns (items, next_token); the first call uses None, and next_token None means the last page.
  • The iterator yields items in order, supports has_next() and peek() and nev...
High-Level Design

State: the current page's items, a position in it, the next token and a done flag. A private _fill loops while the current page is used up and more pages exist, fetching the next one. Using a while ...


Submit Your Answer
Markdown supported