Top Python Interview Questions
Top 50 Python interview questions with curated answers across core concepts, practical scenarios, and interview-ready fundamentals.
Top 50 Python Interview Questions
- How can you wait for multiple tasks but proceed as soon as the first one completes?
- How do mixins relying on `__init__` ensure they don't break the MRO chain?
- How does `object.__init__` behave regarding arguments in Python 3?
- How can you return a lambda function from another function (closure)?
- How can you provide custom IDs for each test case in a parameterized pytest function to make test reports more readable?
- How can you define a custom shortcut (script) in pyproject.toml so that it can be run as 'poetry run my-script'?
- How can you add a dependency that is only used for a specific platform (e.g., Windows)?
- How can you install a package via pip without using the local cache?
- How does CPython handle list growth (amortized complexity) to ensure O(1) appends?
- In a distributed CI/CD pipeline, why is 'pip freeze' alone insufficient for a fully reproducible production build?
- A developer attempts to access a lazy-loaded profile attribute after the session has closed. What error will occur, and why?
- A global application uses `date.today()` to generate invoice dates. Why might this cause consistency issues?
- A developer notices that `datetime.now().microsecond` sometimes returns identical values or 'jumps' in increments larger than 1us. Why?
- A developer is writing code to increment a hit counter in Redis. Why does the following Python code contain a potential concurrency bug?
- A consumer uses `asyncio.gather` inside the message handler for every message. Why can this be buggy for resource management?
- A developer implements a Prometheus counter for HTTP requests. What is the logic bug?
- A developer names their script `json.py` and tries to use the standard library `json` module. Why does the following code crash?
- A data scientist observes a high correlation between two variables and concludes causation. What is the error?
- A developer is using 'manylinux_2_28' to build their wheels. However, users on older versions of Debian (like Debian 10) cannot install them. Why?
- A developer renames their project folder and the venv breaks. Why?
- Compare the different Pytest fixture scopes and discuss the tradeoffs associated with using broader scopes.
- Compare the performance of Async Generators vs. `asyncio.gather` for a sequence of 1000 network requests.
- Compare Time-Based expiration and Explicit Invalidation. Which is preferred for strict consistency?
- Define Cache Hit Ratio and explain why a low ratio is a performance concern.
- Analyze the performance of collections.deque versus a standard list for implementing a FIFO (First-In-First-Out) queue. Why is list.pop(0) considered an anti-pattern in production?
- Can you use a namedtuple as a dictionary key? Explain why or why not based on Python's data model.
- Analyze the time and space complexity of zip(). Why is it considered a 'lazy' operation in Python 3, and how does this differ from Python 2?
- Can you use `with` on multiple items at once? How are they handled?
- Can a closure modify a mutable object in the outer scope without `nonlocal`? Why?
- Compare the cache-aside, write-through, and write-back caching strategies. What are the key trade-offs in consistency vs. latency?
- Analyze the Python import system's caching mechanism. How does `sys.modules` prevent redundant work when a module is imported multiple times in different parts of an application?
- Compare client-side load balancing with server-side load balancing.
- Analyze the memory growth algorithm of Python lists. Why is `append()` considered an amortized $O(1)$ operation despite occasionally triggering a full memory reallocation?
- Describe 'Generational Garbage Collection' in Python and how it optimizes performance.
- Can Python lambda functions contain type hints? If so, how?
- Compare Blue/Green Deployment with Canary Deployment for ML models. Which one is safer for major updates?
- Describe how a 'Service Map' is generated and how it aids in root cause analysis.
- Describe how Poetry's dependency resolver handles conflicts compared to a simple 'pip install'.
- Can you define a fixture that depends on another fixture? How does this establish setup order?
- Analyze the performance characteristics of String Concatenation (`+=`) vs. `str.join()` in Python. Why does building a large string inside a loop using `+=` result in $O(n^2)$ performance?
- Compare and contrast 'Built-in Types' with 'Abstract Base Classes' (ABCs). How do ABCs enforce system boundaries in a large-scale plugin architecture?
- Analyze the directory structure differences of a virtual environment between Windows and POSIX (Linux/Mac). Where do the executables live?
- Analyze the 'Atomic Reference Counting' mechanism in CPython. How does the Global Interpreter Lock (GIL) protect variable reference counts in a multi-threaded environment?
- Compare the memory usage of itertools.chain(list1, list2) versus list1 + list2. Why is chain preferred for large datasets?
- Compare `functools.lru_cache` and `functools.cache`. When should you prefer the former over the latter in a production web server?
- Compare 'Edge-Optimized' versus 'Regional' API Gateway endpoints.
- Compare Elliptic Curve Cryptography (ECC) with RSA. Why is ECC becoming the preferred standard for mobile and IoT devices?
- Can Dataclasses inherit from other Dataclasses? How are fields ordered in the generated __init__?
- Compare and contrast 'Wheels' (.whl) and 'Source Distributions' (sdist). Why has the industry moved toward Wheels as the primary distribution format?
- Compare the performance of Ternary Search and Binary Search. Is Ternary Search always better because it divides the array into three parts?