Implement custom iterator classes
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
##### Question
In a blank coding environment (no IDE assistance, no autocomplete), implement an iterator abstraction entirely from scratch. The interviewer typically asks for this in Java, but the same exercise generalizes to any language with an explicit iterator protocol.
1. **Define the iterator interface manually.** Write the `Iterator` interface yourself (do not import the language's built-in one). At minimum it should expose `hasNext()` (is there another element?) and `next()` (return the next element and advance).
2. **Implement two concrete iterator classes** that implement your interface. For example, one that iterates over a simple backing collection (array or list), and a second one with different traversal semantics (such as a range/generator-style iterator, or one composed over another iterator).
3. **Satisfy three follow-up sub-problems.** During the round the interviewer adds three incremental requirements on top of the basic iterators — typically additional traversal features (e.g. `peek()` without advancing, a `remove()` operation, filtering, or flattening/concatenating multiple iterators) or performance/laziness guarantees (e.g. O(1) `next()`, lazy evaluation, no full-collection copy).
4. **Write runnable unit tests from scratch** that validate each iterator and each follow-up requirement, including edge cases (empty collection, single element, exhausting the iterator, calling `next()` past the end).
Overview: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Implement custom iterator classes states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.