Questions
8 of 38
1What is the Iterator Protocol in JavaScript? What two things must an object implement to be a valid iterator?
2What is the difference between an iterable and an iterator? Can something be both?
3What built-in JavaScript data structures are iterable by default? How does for...of work under the hood with them?
4What does Symbol.iterator do? How would you make a plain object iterable from scratch?
5What is a 'lazy' iterator and why is it important for performance? Give a practical example.
6How would you implement an infinite iterator (e.g., an infinite counter)? How do you safely consume it?
7What happens if you forget to return { done: true } in a custom iterator? What are the consequences?
8How would you implement a reusable iterable (one that can be iterated multiple times)?
9Can you chain or compose custom iterators? Implement a map and filter for a custom iterator without converting to an array.
10What is a generator function? How is it different from a regular function in terms of execution flow?
11Explain what yield does. What does calling .next() return before and after a yield?
12What is the difference between yield and return inside a generator?
13What does yield* do? How is it different from a regular yield?
14Can you pass a value into a generator using .next(value)? How does that work and what is the practical use case?
15Explain generator.return(value) and generator.throw(error). When would you use each in production?
16What happens to a try/finally block inside a generator when .return() is called externally?
17How do generators handle errors? How does .throw() interact with try/catch inside a generator?
18Are generators lazy? How do they differ from eager evaluation with arrays in terms of memory and performance?
19What is the difference between a regular iterator and an async iterator? What protocol does an async iterator follow?
20What does Symbol.asyncIterator do? How does for await...of use it?
21Implement an async generator that fetches paginated API data, yielding one page at a time:
22What are the pitfalls of using for await...of with an async generator that makes network calls? How do you handle errors and cancellation?
23How would you implement a readable stream as an async iterable in Node.js?
24How would you use a generator to implement redux-saga-style side effect management? What makes generators a good fit for this?
25How can generators be used to implement coroutines or cooperative multitasking in JavaScript?
26Implement a take(n) utility that takes the first n values from any iterable — including infinite ones:
27When would you choose a generator over returning an array? What are the memory trade-offs?
28In a data pipeline processing millions of records, how would you use generators to avoid loading everything into memory?
29What are the debugging challenges with generators (e.g., in stack traces and async flows)? How do you mitigate them?
30How do generators compose? Implement a pipeline of generator-based transformations (like RxJS operators but synchronous).
31What are the limitations of generators? What problems are they not a good fit for?
32What are Iterator Helpers (.map(), .filter(), .take(), .drop() on iterators natively)? What stage are they at in the TC39 proposal pipeline?
33How does Array.from() use the iterator protocol? What's the difference between passing an iterable vs an array-like object?
34How do Map, Set, Array, and String expose their iterators? Are they the same object or different?
35What is the difference between map.keys(), map.values(), and map.entries()? What do they return?
36How does destructuring and spread (...) use the iterator protocol internally?
37How would you implement Promise-based async/await using generators and a runner function? (This is essentially how Babel transpiled async/await early on.)
38How would you use a generator to implement a tree traversal (DFS) without recursion stack concerns?
08 / 38

How would you implement a reusable iterable (one that can be iterated multiple times)?

Difficulty: 6/10
iterable protocol, generator functions, stateful iterator

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

  1. 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?
  2. 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?
  3. 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

  1. 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.
  2. 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?
  3. 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

  1. 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.
  2. 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?
  3. 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

  1. 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.
  2. 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.
  3. 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?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.