Implement custom iterator classes

Quick 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.

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.

Coinbase logo
Coinbase
Aug 4, 2025
mediumSoftware EngineerOnsiteCoding & Algorithms
38
0

Implement custom iterator classes

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).

Clarifying Questions to Ask Guidance

  • Clarify input sizes, value ranges, mutability, return format, and tie-breaking.
  • State the target time and space complexity before coding.
  • Call out edge cases such as empty inputs, duplicates, invalid values, overflow, and boundary sizes.

What a Strong Answer Covers Guidance

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions Guidance

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?