Implement custom iterators and interfaces
CoinbaseSoftware Engineer · OnsiteLast 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: (
- FlattenIterator that iterates through a list of lists of integers, skipping empty lists; (
- 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(
- amortized time per element with O(
- or O(depth) extra space as appropriate. Provide brief unit tests demonstrating correctness and discuss time/space complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.