Skip to content
Preptima

Decorators, Generators & Iterators interview questions

Closures, laziness, context managers, and writing composable higher-order utilities.

3 questions in Python.

medium3

mediumConceptCoding

When does a decorator actually run, and what goes wrong when you stack two of them?

Decoration runs once, at definition time, so it happens on import rather than on call. Stacked decorators apply bottom-up and execute outermost-first, and each wrapper replaces the function's identity unless functools.wraps restores it, which is what breaks introspection and framework registration.

5 minmid, senior
mediumConceptCoding

How do generators reduce memory use, and when are they the wrong choice?

A generator produces items on demand and keeps only the current item plus its suspended frame, so memory stays flat as the input grows; it is the wrong tool when you need a length, indexing, or more than one pass, and when per-item resume overhead outweighs the space saved.

5 minmid, senior, staff