Writing Python Interviewers Trust

One real interview problem — parse a log file, report the top-N error codes per service — solved twice and walked side by side: the rushed 26-line function (single-letter names, a bare except, prints, mutates its input) versus the trustworthy version (small functions, guard clauses, honest exceptions, a pure core and a thin I/O edge). The refactor as six trust signals you can name and practise.

Concept · Languages & Runtimes. The source ↗

A free, interactive, animated visual explainer of Writing Python Interviewers Trust — built to be understood, not skimmed.

Questions

What do coding interviewers actually look for in your Python code?
Not cleverness — trust. They already assume you can eventually get a correct answer; what they are deciding is whether your code could be read, tested, and shipped by their team. That judgement is made from how the code reads: whether your names carry meaning, whether functions are small and single-purpose, whether the control flow is flat, whether exceptions are specific, and whether the logic is separable from input and output. PEP 8 states the underlying priority directly — "Code is read much more often than it is written." Two solutions that return the identical answer can score very differently on exactly these signals.
How do you write clean Python code under interview pressure?
Solve it correctly first, then run a short, named refactor you have practised: give variables domain names (service, status_code — not p, s, c); split one long function into small ones that each do one thing; replace nested if-ladders with guard clauses and early returns so the happy path sits flat; catch a specific exception instead of a bare except; and keep a pure core that returns data with a thin edge that does the I/O. Each move is small and each is a signal the interviewer can see. Crucially, narrate the changes out loud as you make them — the reasoning is what is being graded.
Why is a bare except considered bad in Python?
Because it catches too much. A bare "except:" catches BaseException, which includes SystemExit and KeyboardInterrupt — the two things meant to stop your program — as well as every NameError or AttributeError from a typo, so a genuine bug gets silently skipped and the result comes out quietly wrong. PEP 8 is explicit: a bare except clause "will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems." Catch the specific exception you expect (or at least "except Exception", which lets the two exits through) so unexpected errors stay loud.
Should a function print its results or return them?
Return them. A function that prints in the middle of its logic cannot be unit-tested without capturing stdout, cannot be reused to produce JSON or feed another function, and forces the reader to think about the terminal to follow it. The clean pattern is a pure core that takes data and returns data, wrapped by a thin edge that does the input and output. Then you test the core with a plain equality check on its return value, and printing lives in one small place at the boundary. When an interviewer asks "how would you test this?", the pure core is the answer.
How should you name variables in a coding interview?
Name them after the domain, not the mechanism. p[1] forces the reader to reconstruct what index one means; service just tells them. Prefer full words that state the fact (fields, status_code, counts) over single letters, use a leading underscore for a value you deliberately unpack but do not use (_timestamp), and give each extracted function a name that says what it does (parse_line, count_errors_by_service). Good names are the cheapest readability win because the reader spends most of their time reading, not running, your code — which is why the Zen of Python reduces the whole idea to "Readability counts."

Related explainers