Generators interview problems

Generator questions check that you can build lazy pipelines instead of materialising lists. A typical task reads records, filters them, transforms them and batches them, with each stage a generator function that yields. You should be comfortable with yield from, generator expressions, and itertools functions such as islice, chain, groupby, tee, accumulate and batched. Async generators use async def and yield, and are consumed with async for. A generator's finally block runs when it is exhausted, closed with .close(), or garbage collected.

Common mistakes are iterating a generator twice and getting nothing the second time, using itertools.groupby on unsorted input and expecting one group per key, and calling list() in the middle of a pipeline. Another is putting resource cleanup after the loop instead of in try/finally, so it never runs when the consumer stops early. A strong answer knows that generators are single-pass and says so, keeps each stage small and testable, and uses contextlib.closing or aclosing when the consumer might break out early.

1 open problem

  1. 36Delimited Stream Parsermedium

    Turn an iterator of arbitrary byte chunks into delimited messages, handling messages and multi-byte delimiters split across chunks.

8 locked

These open in order of demand. Open one and press I want this one to ask for it.

  1. 18Streaming Inference Limiter Lockedmedium

    Wrap token-streaming backends in an async generator that caps concurrent streams, starts lazily, and frees its slot on every exit path.

  2. 32File Chunk Iterator Lockedeasy

    Write a lazy generator that reads a byte range of a file in fixed-size chunks without ever loading the whole file into memory.

  3. 37Stream Rechunker Lockedeasy

    Regroup a stream of arbitrarily sized byte chunks into fixed-size chunks, lazily and with memory bounded by the chunk size.

  4. 38Stream Until Stop Sequence Lockedmedium

    Forward streamed LLM text until a stop sequence appears, holding back only a possible partial match even when it spans chunks.

  5. 39Streaming JSONL Parser Lockedmedium

    Parse JSON Lines from arbitrary byte chunks into records, handling split lines and UTF-8, with a strict mode and a skip-invalid mode.

  6. 45Stream Idle and Total Timeout Lockedmedium

    Wrap an async token stream so it fails when the gap between items is too long or the whole stream runs past a deadline, closing the source.

  7. 47Token Budget Batching Lockedeasy

    Greedily group inference requests into ordered batches capped by both a maximum request count and a maximum total token count.

  8. 50Streaming Response Aggregator Lockedmedium

    Assemble interleaved token, done and error events into complete responses with a lazy generator that keeps only in-flight requests in memory.