Skip to content
Tech Interview Prep home
Technical interview guide

Comprehensions & Generators

Concise, often faster ways to build sequences — and the lazy-evaluation alternative that avoids materializing them at all.

Read
47 min
Practice MCQs
25
Interview QA
25
Edition
v2
Editorial status
Reviewed

Scope: Python 3.14, including current generator, StopIteration, and asynchronous-generator semantics.

Interview QA

Treat each question like a live interview question: answer out loud first (structure, assumptions, tradeoffs), then open the model answer to spot gaps and rehearse a tighter follow-up.

Curated: · Written: · Reviewed:

QA-1

Compare eager list comprehensions with lazy generator expressions under partial consumption.

QA-2

Translate a multi-clause comprehension into loops and explain the evaluation order.

QA-3

Explain modern comprehension scope and the remaining closure hazards.

QA-4

Distinguish filtering from conditional transformation in comprehension syntax.

QA-5

When should a nested comprehension become explicit loops?

QA-6

How would you detect duplicate keys rather than silently overwrite them in a dict comprehension?

QA-7

Explain the cost and cardinality behavior of a set comprehension.

QA-8

Which portions of a generator expression are eager, and which are deferred?

QA-9

Design an API that avoids accidental reuse of one-shot iterators.

QA-10

Describe the iterable and iterator protocols and their exhaustion contract.

QA-11

How would you provide fail-fast validation around a generator function?

QA-12

What exactly is suspended at a `yield` expression?

QA-13

Explain generator return values and their relationship to `yield from`.

QA-14

Why did PEP 479 change accidental `StopIteration` in generators?

QA-15

Describe a defensible bidirectional protocol built with `send`.

QA-16

How do `throw` and exception handling interact at a suspension point?

QA-17

Design deterministic cleanup for a partially consumed generator.

QA-18

Compare `yield from` with manually iterating over a subgenerator.

QA-19

Audit an iterator pipeline for hidden materialization barriers.

QA-20

How should expected early termination influence eager-versus-lazy design?

QA-21

Which itertools tools can retain significant state, and why?

QA-22

Explain the buffering model and operational risks of `itertools.tee`.

QA-23

How does `groupby` differ from SQL-style global grouping?

QA-24

How do you safely expose or consume an infinite iterator?

QA-25

Choose eager or lazy evaluation for an API and defend the lifetime contract.