Katkat

Implement custom iterators and interfaces

CoinbaseSoftware Engineer · Onsite

Last updated

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.

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.