How to approach scenario questions
Interviewers want to hear how you investigate constraints, make trade-offs, and verify a solution. Use each problem to explain a practical decision process, not just name a language feature.
A clear answer structure
- Clarify requirements and failure conditions.
- Explain the approach and why it fits.
- Cover trade-offs, safety, and edge cases.
- Describe how you would test and measure it.
Real-world problem 1
A job must process a multi-gigabyte CSV without running out of memory. What would you do?
The scenario
A nightly import loads the full file into a list and the worker is killed before the import finishes.
A strong answer
Read the file incrementally with csv.DictReader and process one row or a bounded batch at a time. Validate rows, write results in chunks, and record progress so a failed run can resume. Measure memory use and avoid retaining processed rows.
Real-world problem 2
An API endpoint becomes slow as the number of users grows. How would you investigate it?
The scenario
The endpoint is fast with a small test database but becomes slow in production when returning a list of users and their teams.
A strong answer
Measure request time and database query count first. Look for an N+1 query pattern, fetch related rows in batches or with an appropriate join, add indexes based on the query plan, and paginate the response. Confirm the improvement with production-like data and profiling.
Real-world problem 3
An upstream API intermittently returns timeouts and 503 responses. How should the client behave?
The scenario
A scheduled Python service calls a partner API. Some calls fail temporarily, but retrying every failure can overload the partner.
A strong answer
Set connection and read timeouts, retry only transient failures, and use capped exponential backoff with jitter and a maximum attempt count. Make retries safe with idempotency where the operation changes state. Log attempts and expose a clear failure or fallback when the retry budget is exhausted.
Real-world problem 4
You need to fetch many independent URLs concurrently. Would you use threads, asyncio, or processes?
The scenario
A report gathers results from hundreds of HTTP endpoints and should finish sooner without exhausting sockets or overwhelming the services.
A strong answer
For asynchronous network I/O, use asyncio with an async HTTP client and a semaphore to cap in-flight requests. Use threads when a blocking I/O library cannot be replaced. Reserve multiprocessing for CPU-bound work, and set per-request timeouts and cancellation behavior.
Real-world problem 5
A function unexpectedly reuses values between calls. What Python issue might cause this?
The scenario
Calling add_item(item) multiple times without passing a collection causes later results to include values from earlier calls.
A strong answer
A mutable default argument is created once when the function is defined, then reused. Use None as the default and create a fresh list inside the function. Add a regression test that calls the function repeatedly without an explicit collection.
Real-world problem 6
A long-running worker gradually consumes more memory. How would you debug it?
The scenario
The worker starts with normal memory use, but after processing many tasks it is restarted by the operating system.
A strong answer
Reproduce the growth with representative tasks and compare allocation snapshots using tracemalloc. Look for unbounded caches, global collections, retained closures, and objects held by callbacks. Bound caches, release references after each task, and re-measure under the same workload.
Real-world problem 7
A CPU-heavy transformation is blocking other work. How would you improve throughput?
The scenario
A service spends most of its time resizing and transforming images, and adding asyncio did not improve throughput.
A strong answer
Profile to confirm the work is CPU-bound. Move independent CPU work to a process pool or a suitable native library rather than expecting asyncio to parallelize it. Bound the worker count, monitor queue time and memory, and benchmark end-to-end throughput.
Real-world problem 8
A payment webhook is delivered more than once. How do you prevent duplicate processing?
The scenario
The provider retries a webhook after a timeout, and the same payment event reaches the Python service twice.
A strong answer
Verify the webhook signature, then use the provider event ID as an idempotency key. Enforce uniqueness in durable storage and update the event record and business state in one transaction. Return success for an already-processed event so the provider stops retrying.