A good Python interview answer explains what the code does and checks that explanation with a small input. Start with the questions below about shared lists, dictionary keys, and empty input. For each, predict the result, explain the relevant Python rule, and give one case that could change your answer.
Why did changing one list change another?
If you assign backup = records, both names refer to the same list object. Appending through one name changes the object the other name also refers to. A shallow copy makes a new outer list while keeping references to nested items.
Show the distinction with two levels of data. Then ask whether you need a new outer container, newly constructed records, or no copy at all. That answer depends on who should be allowed to edit each piece.
What makes a useful dictionary key?
A dictionary key must be hashable. An integer or string is a common choice. A tuple can be used if its contents are hashable, while a list cannot. In practice, the key should represent a stable identity for the lookup you are doing.
For a table of readings, ask whether the identity is a sensor name, a timestamp, or the pair. If two readings share a key, mapping[key] = value replaces the earlier value. Describe whether that is an expected update or a sign that the key is too broad.
What does your solution do with an empty input?
Consider a function that returns the average of a list. There is no denominator when the list is empty. Your interface could reject the input, return a value that signals missing data, or handle the empty case higher up. State which convention you choose and how the caller detects it. Returning zero silently would mix “no readings” with a real average of zero.
Now pick a normal case with an answer you know. If the values are 4 and 8, say you expect 6. Add an empty-case test that verifies the behavior you described. You have shown both the usual route and the contract boundary.
When would you catch an exception?
Catch the exception at the point where your program can make a meaningful choice. For a missing optional configuration file, you might use a documented default. For a malformed configuration, returning the same default may hide an error the caller needs to fix. A narrow handler lets the two cases remain different.
If asked to debug a traceback, identify the failing operation and its actual inputs. Mention what additional value or type you would inspect. A short diagnostic plan is more informative than naming every potential problem.
How should you explain performance?
Name the variable that controls the size of the work. If a program tests every pair of rows, talk about the number of comparisons as the row count grows. Then discuss alternative constraints. A lookup table may reduce repeated searches at the cost of extra memory, while an ordered stream may let you compare adjacent items.
Practice with a timer only after you can explain the operation count. When measuring, run the same input and record the environment. One fast run alone says little about the shape of the work.
Use one example and one edge case in each answer
Use a short sequence: restate the input, give one expected result, explain the relevant Python rule, and check an edge case. If the interviewer changes a requirement, revise the test before revising the solution. This keeps the conversation about what the code should do, even when you forget a method name.
What changes when the input is an iterator?
A list supports repeated iteration over its values. A typical iterator yields the next value and advances its state. If it has already been exhausted, another loop over the same iterator has no values to consume. This matters when code prints a preview by reading the input and then assumes the input is still untouched.
For an interview, make the issue observable with a short iterator.
records = iter(["ready", "review"])
print(next(records)) # ready
print(list(records)) # ['review']
print(list(records)) # []
Ask whether the caller can create a fresh stream or whether the function needs to produce results in one pass. If you propose converting to a list, discuss how much data could arrive. Two records fit easily. An open-ended input can make that operation unsuitable.
Why can a function remember an earlier call?
Python evaluates default argument expressions when the function definition runs. A mutable default, such as an empty list, can therefore retain changes across calls that leave out the same argument. To create a new list for each omitted argument, use a sentinel such as None and initialize inside the function.
The follow-up question is often about caller expectations. If a caller gives you a list, should your function edit it or produce a new list? Both are possible. Write your function name, documentation, or test so the behavior is apparent.
How would you review a working solution?
Read the code from the entry point and identify where the result is created. Look for an assumption that never got a test. If a function returns the first matching record, ask how it handles no match and how callers know the difference between “missing” and a match whose value happens to be empty.
Try a changed requirement. If “first match” becomes “all matches in input order,” the name and return type may need to change as well. A reviewer can often learn more from how you make that adjustment than from a correct answer to the original prompt.
When you rehearse, keep a short list of assumptions you discovered. Focus your next session on the ones you missed, such as whether a function mutates an input, which errors it should propagate, or how many times it reads the data. That creates a practice plan tied to decisions you will make in real code.
When you can explain these examples out loud, practice applying this approach to a coding challenge.