Python Core Interview Cheat Sheet for a Senior Interview
1.
Python Core —1-Page Interview Cheat Sheet
Global Interpreter Lock (GIL)
Ensures only one thread executes Python bytecode at a time.
Simplifies memory management via reference counting.
CPU-bound threads do not run in parallel; I/O-bound threads benefit from GIL release.
Workarounds: multiprocessing, asyncio, native extensions.
Python Memory Model
Private heap managed by interpreter.
Reference counting + cyclic garbage collector.
Memory leaks caused by reference cycles with __del__, globals, C extensions, unbounded caches.
Context Managers
Define setup/teardown logic using with-statement.
Implemented via __enter__/__exit__ or contextlib.contextmanager.
Guarantee resource cleanup even on exceptions.
Decorators
Functions that wrap or modify other functions.
Used for logging, auth, caching, retries, metrics.
functools.wraps preserves metadata and introspection.
Generators
Iterators using yield for lazy value generation.
Memory-efficient, suitable for large datasets and pipelines.
yield pauses execution; return terminates generator.
Mutable vs Immutable Types
Immutable: int, float, str, tuple, frozenset (hashable, thread-safe).
Mutable: list, dict, set, custom objects (in-place modification).
Mutable default arguments are evaluated once → shared state bug.
System Design Impact
GIL → concurrency model choice.
Memory model → leak prevention.
Context managers → safe resource handling.
Decorators → clean cross-cutting concerns.
Generators → scalability and performance.
Immutability → correctness and safety.
Senior One-Liner
Most Python production bugs stem from misunderstanding concurrency, mutability, or resource
lifecycles—not syntax.