Python interview questions and answers, from data structures to concurrency
Strong Python answers connect the language rule to a production consequence. Do not stop at saying that lists are mutable or that Python has a GIL: explain what can change unexpectedly, which queue operation becomes slow, or why an I/O-bound job differs from CPU-bound work. The answers below are checked against the Python data-structures tutorial and the threading documentation.
How Python questions appear in interviews
In a coding round, the interviewer may care less about a perfect trick than about whether you choose a readable data structure, state complexity correctly, and test the edge cases. Pair this guide with technical interview preparation, live coding help, and the guide to explaining code out loud.
Junior Python interview questions
1. What is the difference between a list and a tuple?
A list is mutable; a tuple is immutable. Use a list when you intend to change the collection and a tuple when the fixed shape communicates an invariant. Immutability can also make a value usable as a dictionary key when all of its members are hashable.
2. Why are mutable default arguments risky?
Default argument expressions are evaluated once when the function is defined, not once per call. A default list or dictionary can therefore be shared by later calls. Use None as the default and create a new list or dictionary inside the function.
3. What is the average lookup cost of a dictionary?
A dictionary is a hash table, so lookup, insertion and deletion are average-case constant time. Say average-case, not guaranteed constant time: hash collisions and resizing exist, and a key must be hashable.
4. When would you use a set?
Use a set for membership tests, deduplication and set operations such as intersection. It expresses intent better than repeatedly scanning a list, but it does not preserve a meaningful insertion order for algorithms that need an ordered result.
5. Why use deque instead of a list as a queue?
Removing from the front of a list shifts later elements and is slow for a queue. collections.deque is designed for efficient appends and pops at both ends, so it is the better FIFO choice.
Mid-level Python interview questions
6. What is a generator?
A generator yields values lazily instead of building the full result in memory. It is useful for streams, large files and pipelines. The trade-off is that an iterator is normally consumed once, so materialise it only when repeated access is actually needed.
7. What is the difference between an iterable and an iterator?
An iterable can produce an iterator, usually through iter(). An iterator keeps traversal state and returns items through next() until it raises StopIteration. A list is iterable; its list iterator is the stateful iterator.
8. What does a decorator do?
A decorator takes a function or class and returns a wrapped or modified version. It is a useful way to add cross-cutting behaviour such as logging, authentication or registration, but the wrapper should preserve metadata with functools.wraps when introspection matters.
9. What is a context manager?
A context manager defines setup and cleanup around a with block. Files, locks and transactions are common examples. Its value is that cleanup happens even when the block raises, rather than relying on every path to remember a manual close.
10. When should you use a dataclass?
Use a dataclass for a value-like object whose main job is carrying named fields. It can generate an initializer, representation and equality methods. Do not choose it merely to avoid writing a class when the object has complex lifecycle or invariants that deserve explicit code.
Senior Python interview questions
11. What does the GIL change?
In standard CPython builds, the Global Interpreter Lock means only one thread executes Python bytecode at a time. Threads are still useful for I/O-bound tasks because waiting on I/O can release the GIL. For CPU-bound Python work, consider processes or native code; free-threaded builds are a separate deployment choice, not a universal assumption.
12. How do threads, processes and asyncio differ?
Threads share a process and are practical for blocking I/O. Processes have separate memory and can use multiple cores, at the cost of startup and data transfer. asyncio coordinates cooperative non-blocking I/O in one thread. Choose from the workload and libraries, not from fashion.
13. How do you prevent a blocking call from freezing an async service?
Use an asynchronous library when one exists, move genuinely blocking or CPU-heavy work to a worker or process pool, and set timeouts. Marking a normal blocking function async does not make it non-blocking.
14. What is the difference between shallow and deep copy?
A shallow copy creates a new outer container but keeps references to nested objects. A deep copy recursively copies nested values. Prefer making ownership clear over blindly deep-copying large objects, because deep copies can be expensive and can conceal a design problem.
15. How do you make a Python service easier to test?
Keep business logic in small functions with explicit inputs and outputs, isolate I/O behind boundaries, inject clocks or clients where practical, and test error paths and invariants. A fast unit test suite catches more than a single end-to-end happy path.
How to answer a Python follow-up
State the data structure or execution model, its complexity or boundary, and one trade-off. If a prompt is ambiguous, ask whether the workload is CPU-bound or I/O-bound, whether ordering matters, and how large the input can become. That is stronger than choosing an abstraction before understanding the problem.
Ask the recruiter what tools are permitted in a live assessment. Language documentation and an AI assistant are separate policy decisions from knowing Python itself.
Sources
- Python documentation — official documentation
FAQ
What Python interview questions are most common?
Expect mutability, lists versus tuples, dictionaries and sets, generators, iterators, decorators, context managers, exceptions, testing, typing, the GIL, asyncio, threads and processes.
How should I explain the GIL in an interview?
In standard CPython builds, one thread executes Python bytecode at a time. Threads can still help I/O-bound work; CPU-bound Python work often needs processes, native code or another explicit parallelism strategy.
Why is deque better than a list queue?
Popping from the front of a list shifts remaining elements. collections.deque is designed for fast appends and pops at both ends, so it is a better FIFO queue.
When should I use a generator?
Use a generator when values can be produced lazily, such as a file stream or a large pipeline. Remember that iterators are normally consumed once.
What should I ask before coding a Python solution?
Clarify input size, ordering, duplicate handling, failure behaviour and whether the workload is CPU-bound or I/O-bound. Those constraints determine the right data structure and concurrency model.