Custom Iterators
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-03-13
Problem Statement
In many large-scale applications (data processing engines, query systems, distributed datasets, graph databases, or in-memory caches), iteration over data structures is a fundamental need. While built-in iterators provide sequential access, real-world systems often require custom iterators that support:
•Filtering, transformations, and projections.
•Iteration over composite/heterogeneous collections.
•Efficient pagination and parallel traversal.
•Stateful behavior (e.g., checkpointing, resumability).
You are asked to design a scalable Custom Iterator System that can be used by multiple teams to traverse large datasets safely, flexibly, and efficiently.
Functional Requirements
1.Iteration Basics
•Support standard operations: hasNext(), next().
•Must handle nested or composite data sources (e.g., list of lists, streams, graph edges).
2.Custom Behaviors
•Filtering (e.g., skip nulls, only even numbers).
•Transformation (map input element → new structure).
•Merging / Zipping multiple iterators.
•Random access (optional, like get(index)).
3.Advanced Use Cases
•Pagination & Batching → Iterate over huge data without loading everything into memory.
•Checkpointing → Ability to resume from the last processed element.
•Thread Safety → Multiple consumers can safely iterate (or clone iterators).
•Streaming Data Sources → Support iteration over Kafka-like streams or APIs.
4.Extensibility
•Easy to add new iterator types (e.g., GraphIterator, DBIterator, NetworkIterator).
•Composable design so users can build pipelines (e.g., FilterIterator -> TransformIterator -> BatchingIterator).
Non-Functional Requirements
•Performance: Must handle millions of elements efficiently.
•Memory Efficiency: Should not load entire dataset into memory at once.
•Scalability: Support distributed iterators (remote shards, partitions).
•Consistency: If iterating over mutable datasets, define snapshot semantics.
•Observability: Logging/metrics for debugging long-running iterations.
Example Scenarios
1.DB Query Iterator → Iterate over query results with LIMIT/OFFSET internally.
2.Graph Traversal Iterator → BFS/DFS style iteration over graph nodes.
3.Log Streaming Iterator → Consume live logs with backpressure handling.
4.Composite Iterator → Given a list of lists, flatten into a single iterator.
High-Level Design Considerations
•Iterator Interface Layer → Defines common contract (hasNext, next, peek, etc.).
•Iterator Wrappers → Implement filtering, transformation, batching as decorators.
•Data Source Adapters → Custom iterators for DB, cache, APIs, streams.
•Concurrency Handling → Locking, copy-on-write, or snapshot-based strategies.
•State Persistence → Store iterator state (offset, cursor) for resumability.
•Pipeline Execution → Chaining iterators into execution graphs (like Java Streams or Flink).
Discussion Points for Interview
•How would you design thread-safe iterators?
•How would you support parallel iteration (splitting workload across workers)?
•How would you handle infinite streams where hasNext() never returns false?
•How would you implement resumability/checkpointing efficiently?
•Tradeoffs between lazy evaluation vs eager fetching in iterators.