Python Senior interview questions and answers
75 questions for a Python (Senior) interview - with correct answers, explanations and links to the docs.
- 1.How does CPython free most objects?
- 2.Why does the cyclic garbage collector use generations?
- 3.What risk comes with defining
__del__on a class? - 4.When is
weakrefthe right tool? - 5.What are immortal objects in CPython 3.12+?
- 6.What is the difference between a data descriptor and a non-data descriptor?
- 7.How do plain functions become bound methods?
- 8.What is the difference between
__getattr__and__getattribute__? - 9.What does
__set_name__give a descriptor? - 10.What is a metaclass?
- 11.Which hook often replaces a metaclass for registering subclasses?
- 12.When do you need to override
__new__instead of__init__? - 13.Which side effect does
__slots__have beyond saving memory? - 14.What does
dis.dis(func)show? - 15.What did the specializing adaptive interpreter in 3.11 change?
- 16.What does the free-threaded CPython build (PEP 703) change?
- 17.On a free-threaded build, does
counter += 1from many threads stay correct? - 18.Why can NumPy speed up with threads even on a GIL build?
- 19.Why is the
forkstart method risky in a multithreaded process? - 20.What has to be true about arguments sent to a
ProcessPoolExecutor? - 21.What happens when an asyncio task is cancelled?
- 22.What does
asyncio.TaskGroupadd overgather? - 23.How do you handle errors from an
ExceptionGroupby type? - 24.Why keep a reference to a task created with
asyncio.create_task? - 25.How do you limit an asyncio crawler to 10 concurrent requests?
- 26.What is the right way to call a blocking library from async code?
- 27.Why are
contextvarsused instead ofthreading.localin async code? - 28.How can you find what blocks the event loop?
- 29.A service's memory grows steadily. Which tool shows where Python allocates it?
- 30.Why is
@lru_cacheon an instance method a memory risk? - 31.Which profiler suits a production process you can't restart?
- 32.What happens when a list's
appendneeds more capacity? - 33.Why does
hash("abc")differ between runs of Python? - 34.How are modern CPython dicts laid out?
- 35.What does
typing.ParamSpeclet a decorator express? - 36.What does PEP 695 syntax
def first[T](items: list[T]) -> T:replace? - 37.What is
@typing.overloadfor? - 38.What does
typing.Selfexpress as a return type? - 39.What does
@runtime_checkableon a Protocol allow? - 40.What changes with deferred annotation evaluation in Python 3.14 (PEP 649)?
- 41.What do template strings (
t"...", PEP 750) return? - 42.Why is
pickle.loadson untrusted data dangerous? - 43.Why avoid
subprocess.run(f"convert {name}", shell=True)? - 44.Which module should generate password reset tokens?
- 45.What is the difference between a wheel and an sdist?
- 46.Why shouldn't a library pin exact dependency versions like
requests==2.32.3? - 47.What does
python -X importtime app.pyhelp with? - 48.Why are import-time side effects in a library a problem?
- 49.How should a library tell users that a function will be removed?
- 50.What does
__match_args__on a class enable? - 51.Why does
case RED:in amatchnot compare with a constantRED? - 52.What changed about
functools.cached_propertyin 3.12? - 53.How do you run a FastAPI app across several CPU cores?
- 54.Why must a DB connection pool be created after a worker process forks?
- 55.How do you prevent two requests from updating the same Django row with a lost update?
- 56.What is the risk of sending a Celery task inside a DB transaction?
- 57.Why should Celery tasks be idempotent?
- 58.When does an async Django view not help?
- 59.What does property-based testing with Hypothesis add over example tests?
- 60.Why use
create_autospecorautospec=Truewhen mocking? - 61.Tests share a module-level cache and fail only in some orders. What's the best fix?
- 62.What does
sys.getrefcount(obj)usually return compared to the real number of references? - 63.What did per-interpreter GIL (PEP 684, 3.12) enable?
- 64.What is the status of CPython's JIT compiler as of 3.13 and 3.14?
- 65.What does
add_note()on an exception do? - 66.A hot loop calls
obj.methodmillions of times. What micro-optimization does CPython 3.11+ make less useful? - 67.When is moving a hot path to Rust (PyO3) or Cython worth it?
- 68.Why does
timeitturn off garbage collection by default? - 69.Why is mutating a list while iterating over it a bug?
- 70.How do you make sure all asyncio tasks finish cleanly on SIGTERM?
- 71.What does
asyncio.timeout(5)do around a block? - 72.Why can't you use the ORM from an async Django view directly?
- 73.What does
uvmainly replace in a Python workflow? - 74.What is the trade-off of strict mypy in an existing large codebase?
- 75.Why can deeply recursive code fail in Python where it works in other languages?