In a Python coding interview, a solution can pass the first example and still fail on the second call. The algorithm may be fine: a default list survived, a row was shared, or an iterator was already exhausted. Practice tracing those cases before typing a fix. For each one, predict the result, explain what Python is doing, and say whether the behavior is intentional.

Start by tracing objects, not variable names

Names can refer to the same object. Assignment can bind a name to another object, while mutation changes an object that other names may still refer to. Before walking through a snippet, mark the objects that exist, which references point to them, and which line changes those relationships.

1. Why did changing one grid cell update every row?

grid = [[0] * 2] * 3
grid[0][1] = 7
print(grid)
print([[0] * 2 for _ in range(3)])

The first print produces [[0, 7], [0, 7], [0, 7]]. Repetition makes three references to one inner list. The comprehension evaluates its row expression three times and creates independent rows. Calling grid.copy() would only copy the outer list. As a follow-up, explain why repeating the integer zero inside each row is fine here: the assignment replaces one reference in the row; it does not mutate an integer.

2. Why does a second call remember the first?

def record(value, history=None):
    if history is None:
        history = []
    history.append(value)
    return history

With history=[] as the default, that list would be created when the function definition executes, not when each call begins. The version above creates a fresh list for an omitted history and preserves an explicitly supplied list, even if it is empty. Do not write history = history or [] if the caller expects an empty list to be updated in place. If mutation of an explicit argument is undesirable, copy that argument and explain what a shallow copy does not protect.

3. When should you use identity rather than equality?

Use is when you mean the same object, for example when checking value is None. Use == to ask whether values compare equal. Two lists constructed separately as [1, 2] compare equal but are distinct objects. Do not infer identity rules for strings or integers from one interpreter session. For an API where None is a valid explicit value, a private sentinel created with object() can distinguish omission from an explicit None.

Then trace which values are consumed

4. Why is the second pass over a generator empty?

values = (n * n for n in range(4))
print(next(values))
print(list(values))
print(list(values))

This prints 0, then [1, 4, 9], then []. A generator expression produces an iterator and computes results as they are requested. The first call consumes one value, and the first conversion consumes the rest. A list can be iterated again because each new iteration can get its own iterator. If two passes are necessary, decide whether to materialize the data, recreate the input when possible, or redesign the computation so one pass is enough. An iterator backed by a network stream may not be replayable.

5. Why not build a set every time you need membership?

A set is a good starting point when you need repeated membership tests against hashable items. Building it takes time and memory, so a single check on a short list may not benefit. A set also does not preserve the input's order as a guarantee. For “return the first repeated item,” scan the original input once and keep a set of items seen so far; a set of the entire input cannot tell you where a repeat first occurred. If the items are dictionaries, decide what key defines “the same” rather than trying to insert the dictionaries directly.

Practice by changing one constraint

Try a small exercise: return unique user IDs in input order without mutating the input. Start with a list of IDs, then change the input to an iterator of user records, and look at which parts of your solution stay the same. Keep a membership set and use a stable, hashable ID as its key. Build an ordered result if the caller needs to reuse it; consider yielding IDs instead if the result should be consumed lazily. Yielding avoids storing the output, but the set still grows with the number of unique IDs. That difference matters more than simply calling the solution “streaming.”

  • Trace an empty input, an input with no duplicates, and a repeat at the end.
  • Call your function twice with different inputs and confirm that no state leaks.
  • Explain what happens if two user records have the same ID but different details.
  • State the memory trade-off and name one situation where it would matter.

Practice recalling an explanation instead of rereading it, then try Mentaro's free practice demo to rehearse answering before revealing the explanation. The demo uses sample questions and does not require an account.

Sources