Iterate Over Nested Order Bundles as a Flat Item Stream

Last updated: November 29, 2026

Quick Overview

An order can contain items and bundles, and bundles can contain further items and bundles to any depth. Implement an iterator with next() and has_next() that yields the individual items in order, lazily, without flattening the whole structure up front.

Amazon
Software Engineering Fundamentals
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


An order can contain items and bundles, and bundles can contain further items and bundles to any depth. Implement an iterator with next() and has_next() that yields the individual items in order, lazily, without flattening the whole structure up front.

A fundamentals question in the style of Amazon technical screens that tests the iterator protocol and stack-based traversal. The interviewer cares about laziness (memory proportional to depth, not size), correct handling of empty bundles, and has_next being idempotent.

What the Interviewer Expects
  • Keep a stack of iterators rather than a flattened list
  • Make has_next do the work of finding the next item and be idempotent
  • Skip empty bundles correctly
  • Implement the Python iterator protocol too
  • State memory in terms of nesting depth
Key Topics to Cover
Iterators
Stacks
Lazy Evaluation
Recursion Elimination
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you support remove() on the current item?
  • How would you make it thread safe for concurrent consumers?
  • What if bundles can reference each other and form cycles?
Sharpen Your Skills on Codemia

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

Practice System Design Problems
Sample Answer
Requirements

Input is a list whose elements are either items (non-list values) or nested lists (bundles). Empty bundles are allowed. next() raises StopIteration when exhausted....

High-Level Design

Maintain a stack of Python iterators, starting with iter(root). has_next() loops: peek at the top iterator and pull its next element; if that iterator is exhausted, pop it; if the element is a bun...


Submit Your Answer
Markdown supported