Implement custom iterators and interfaces

Read the full interview experience this question came from →

Quick Overview

Implement custom iterators and interfaces evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Implement custom iterators and interfaces

Company: Coinbase

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Define a minimal Iterator interface (e.g., hasNext(), next()) for integers without using IDE-generated scaffolding. Implement two iterator classes in Java: ( 1) FlattenIterator that iterates through a list of lists of integers, skipping empty lists; ( 2) FilterIterator that wraps any Integer iterator and yields only elements satisfying a provided predicate. Both must be robust to edge cases (empty input, single element, repeated hasNext() calls), throw appropriate exceptions when next() is invalid, and operate in O( 1) amortized time per element with O( 1) or O(depth) extra space as appropriate. Provide brief unit tests demonstrating correctness and discuss time/space complexity.

Overview: Implement custom iterators and interfaces evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Read the full Coinbase Software Engineer interview experience this question came from

Coinbase logo
Coinbase
Jul 31, 2025
mediumSoftware EngineerOnsiteCoding & Algorithms
24
0

Implement custom iterators and interfaces

Define a minimal Iterator interface (e.g., hasNext(), next()) for integers without using IDE-generated scaffolding. Implement two iterator classes in Java: (

  1. FlattenIterator that iterates through a list of lists of integers, skipping empty lists; (
  2. FilterIterator that wraps any Integer iterator and yields only elements satisfying a provided predicate. Both must be robust to edge cases (empty input, single element, repeated hasNext() calls), throw appropriate exceptions when next() is invalid, and operate in O(
  3. amortized time per element with O(
  4. or O(depth) extra space as appropriate. Provide brief unit tests demonstrating correctness and discuss time/space complexity.

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?