Top 10 Python scenario questions
Try each one yourself first, then open the model answer.
1. A function with a default argument of an empty list keeps returning items from earlier calls. What is happening and how do you fix it?
Show answerHide answer
Default values are evaluated once, when the function is defined, so every call shares the same list. Use None as the default and create a new list inside the function when the argument is None.
2. Your script reading a 20 GB log file runs out of memory. What would you change?
Show answerHide answer
Stop loading the whole file with read() or readlines(). Iterate over the file object line by line, and chain generators for filtering and parsing so only a small part is in memory at any time.
3. You added threads to speed up a CPU-heavy calculation and it got slower. Why, and what would you do?
Show answerHide answer
In standard CPython the global interpreter lock lets only one thread execute Python bytecode at a time, so CPU-bound threads add overhead without parallelism. Use multiprocessing or a process pool, or move the hot loop into a native library such as NumPy. Free-threaded builds change this, but check that your interpreter and dependencies support them.
4. An API service must call 500 slow external endpoints per request batch. How would you design it?
Show answerHide answer
This is I/O-bound work, so use asyncio with an async HTTP client, or a thread pool. Limit concurrency with a semaphore, set timeouts, retry with backoff only for safe requests, and collect errors without failing the whole batch.
5. Two modules import each other and you get an ImportError. How would you solve it?
Show answerHide answer
It is a circular import. The best fix is to move the shared code into a third module so dependencies point one way. Importing inside the function that needs it works as a short-term measure.
6. You modify a list while looping over it and some items are skipped. What do you do?
Show answerHide answer
Removing items shifts the indexes under the iterator. Build a new list with a comprehension, or loop over a copy and modify the original.
7. A colleague compares strings with `is` and it works in tests but fails in production. How do you explain it?
Show answerHide answer
`is` checks whether two names refer to the same object, while == checks equal values. Some strings happen to be reused by the interpreter, which hides the bug. Use == for values and keep `is` for None and other singletons.
8. A database connection stays open whenever an exception is raised. How would you prevent this?
Show answerHide answer
Manage it with a context manager (a with statement) or try/finally, so cleanup runs on every path. You can write your own with contextlib.contextmanager.
9. A report endpoint recalculates the same expensive result on every call. What options do you have?
Show answerHide answer
Cache it. functools.lru_cache or cache works for pure functions in one process. For several servers use a shared cache such as Redis, with an expiry and a plan for invalidating stale data.
10. You join a project with no tests and must change a critical function. How do you proceed?
Show answerHide answer
First write tests that capture the current behaviour, including edge cases, then change the code in small steps. Add type hints where they clarify intent and run a linter and type checker in CI.
