A reusable iterable returns a fresh iterator each time [Symbol.iterator]() is called, resetting the state for each iteration.
Reusable iterable
Scenario Questions
0-2 years experience
1We need a utility that returns an object you can use in a for...of loop to iterate over the numbers 1 through 5, and you should be able to loop over it multiple times. How would you implement that?
2Imagine a simple class `Range` that should let callers do `for (const n of new Range(1,3))` and then later reuse the same instance in another loop. What code would you write to make it reusable?
3If you wrote a generator function `function* numbers(){...}` and called it once, why does the resulting object only work for a single loop? How would you change it so the same function call can be used in multiple loops?
2-5 years experience
1Your team added a `PaginatedResult` class that implements Symbol.iterator to lazily fetch pages from an API. After a bug, the second consumer of the same instance sees no items. Walk me through why that might happen and how you'd fix it to make the iterable reusable.
2We have a feature that streams log entries using a custom iterator. The iterator holds internal buffers. When the component is re‑mounted, we need to iterate again without creating a new object. How would you redesign the iterator to support resetting or recreating safely?
3Suppose you need to expose a reusable iterable of user IDs that can be filtered on the fly. How would you structure the iterable to allow multiple independent loops while keeping the underlying data source lazy?
5-8 years experience
1Design a reusable iterable for processing large CSV files line‑by‑line in a Node.js service. Discuss how you'd implement Symbol.iterator, handle back‑pressure, ensure each iteration is independent, and what trade‑offs exist regarding memory and I/O.
2Our analytics pipeline uses a shared iterator to walk through a stream of events. Under high load, multiple workers need to iterate over the same logical sequence without interfering. How would you architect a reusable iterable or iterator pool to guarantee isolation and performance?
3Explain how you would extend a reusable iterable to support both synchronous and asynchronous iteration (i.e., Symbol.iterator and Symbol.asyncIterator) while preserving reusability. What challenges arise and how would you address them?
8+ years experience
1Our platform is migrating legacy code that uses one‑off iterator objects to a new reusable iterable API across many services. As the lead architect, outline the migration strategy, compatibility layers, and how you'd enforce the reusable contract at compile‑time or via linting.
2Consider a micro‑service ecosystem where several services expose data via reusable iterables over network calls. Discuss the architectural implications, versioning, and how you would design a cross‑service contract that ensures each consumer can safely iterate multiple times without side effects.
3If you had to build a library that provides reusable iterables for various data structures (arrays, trees, graphs) and wants to guarantee O(1) reset and minimal allocations, what design patterns and TypeScript typings would you employ, and how would you evaluate the long‑term maintenance impact?
Follow-up Questions
What would happen if you returned the same iterator instance each time?
How would you modify your design to support infinite sequences without leaking memory?
Can you explain the difference between an iterator and an iterable in this context?
Sharethis question
Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.