Senior · Design · Lazy sequence ownership

Design a lazy API over files or network records with cancellation and explicit ownership. Decide whether callers can enumerate twice, which exceptions escape during iteration and whether a snapshot alternative is worth its allocation and early resource release.

Think through your answer before opening the explanation.

Coding interviews

Reveal answer and reasoning

A resource-backed API should document that the returned enumerator owns the open resource until disposal and should support cancellation at appropriate asynchronous operations. A snapshot alternative can close resources before returning at the price of retained data. Neither choice is universally superior; it depends on result size and consumer ownership discipline.

For p yielded pairs, work is O(p) with constant auxiliary iterator state, excluding resources owned by the sources. Because the condition advances the left side first, exhaustion of the right side can consume one extra left value. That detail matters for one-shot or side-effecting inputs. Disposing an enumerable object is not the same as disposing the enumerators it creates. Returning a lazy query also extends source-resource lifetimes beyond method return.

This sample does not make non-repeatable streams replayable or promise that no source side effect occurs during MoveNext.

A rigorous interview explanation identifies acquisition time, disposal responsibility and the exact effect of early Take. It derives the eleven result and both cleanup flags from actual iterator control flow. It also notices the shorter-input extra-read asymmetry, a practical issue hidden by many simple Zip-like examples. A senior answer distinguishes the enumerable recipe from each acquired enumerator and considers a snapshot alternative when callers cannot safely retain resources.

This lesson therefore tests lifecycle reasoning and API contracts, while using ordinary BCL iterators that run in the portable console environment.

What a strong answer covers

  • A resource-backed API should document that the returned enumerator owns the open resource until disposal and should support cancellation at appropriate asynchronous operations.
  • For p yielded pairs, work is O(p) with constant auxiliary iterator state, excluding resources owned by the sources.
  • Disposing an enumerable object is not the same as disposing the enumerators it creates.

Common pitfall

Disposing an enumerable object is not the same as disposing the enumerators it creates. Returning a lazy query also extends source-resource lifetimes beyond method return. This sample does not make non-repeatable streams replayable or promise that no source side effect occurs during MoveNext.

Follow-up questions

  1. Use a shorter right sequence and count left-side MoveNext observations. Hint: left is evaluated first in the short-circuit condition. Then stop before the second output and verify both finally blocks still run.
  2. Which source can be advanced one extra time when the other source is exhausted, and why?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions