KatInterview

Python Deep Dive: Concurrency, Generators, Typing and Memory

Data Structure DesignModel answer
MediumModel answerData Structure Designpythongilconcurrency

Last updated

Nine-part Python fundamentals review covering the GIL, concurrency models, decorators, generators, typing, packaging, profiling, memory management and exceptions.

You are given a Python fundamentals round. Prepare a clear, spoken-length answer (roughly one to two minutes each) for every item below, with a short code snippet where one is asked for.

  1. The GIL. What is it, and how does it affect CPU-bound versus I/O-bound workloads?
  2. Threads, processes, asyncio. Compare the three and give a scenario and a short snippet for each.
  3. Context managers and decorators. Write one of each from scratch and name typical uses.
  4. Iterators and generators. Cover yield from, what happens when a generator is abandoned, and how to get backpressure.
  5. Static typing. Discuss PEP 484 hints, dataclasses and a checker such as mypy, and explain how they help maintainability.
  6. Packaging and environments. Cover virtual environments, pip, wheels, and how to make dependency installs reproducible.
  7. Performance. Name common pitfalls, show how to profile (cProfile, line_profiler), and list optimization options.
  8. Memory management. Explain reference counting and the cyclic collector, and how leaks still happen.
  9. Exceptions. Describe good handling practices and how to design custom exception types.

Be explicit about assumptions (CPython, version), mention trade-offs, and say how you would verify each claim.

Hints

Hint 1

For the GIL, think about when the running thread gives it up: blocking system calls and some C extensions release it, pure-Python bytecode loops do not.

Hint 2

For generators, ask what is still paused inside the frame when the consumer walks away, and which exception is raised at the paused yield when it is closed.

Hint 3

For leaks, remember that reference counting frees most objects immediately, so look for something that keeps a reference alive: module-level caches, closures, registered callbacks, or unbounded collections.