Interview Q&A · questions with model answers
Python Developer Interview Questions
Part of the study plan for: Full Stack Engineer · Native AI Engineer · Forward Deployed Engineer (FDE) · Machine Learning Engineer · Data Engineer
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
How do you profile and optimize Python code?
Measure first: cProfile/profile for function-level CPU time, py-spy or Scalene for low-overhead sampling (even in production), line_profiler for hot lines, tracemalloc/memray for memory, and timeit…
-
How do you mock correctly in Python tests?
Use unittest.mock.patch on the name where it's looked up (the module under test), prefer autospec=True so mocks match real signatures, assert on calls (assert_called_once_with), and prefer injecting…
-
How do you run external commands safely with `subprocess`?
Use subprocess.run([...args], check=True, capture_output=True, text=True, timeout=...) with an argument list (no shell), which prevents shell injection. Avoid shell=True with user input, and handle…
-
How do you implement structured logging in Python services?
Configure logging once (dictConfig) with a JSON formatter (python-json-logger) or use structlog, include context (request IDs via contextvars), log exceptions with logger.exception, and send logs to…