PyCodeItPython trace & interview prep
python

Top 10 Trace Table Interview Questions

Ten compact, high-value trace questions with step-by-step answers you can practice in ten minutes a day.

AA

Ameer Abdullah

Data Science & AI/ML Graduate-GitHub-Dev.to

9 min read

This article collects ten small trace problems frequently seen in technical screenings. Each includes a short explanation so you can check your mental model quickly.

1. Mutable default accumulator:
def add(x, lst=[]):
    lst.append(x)
    return lst
Explain why multiple calls accumulate values and how to fix it.

Answer: The default list is created at function definition time and reused across calls. Fix by using `None` and creating a new list inside the function when needed.

2. Late-binding lambda in loop:
funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])

Answer: [2, 2, 2] because lambdas capture the variable `i` by reference and evaluate when called. Fix by capturing the value with a default parameter: `lambda i=i: i`.

3. Generator exhaustion: calling next() after the final yield raises StopIteration. Always consider generator state in your trace.

4. Comprehension scope: in Python 3, comprehension variables do not leak to the outer scope; trace them as separate temporary frames.

5. Exception flow: when an exception occurs, stop normal lines and unwind frames until a matching handler is found. The assignment that raised does not complete.

6. Mutable aliasing: assignments do not copy objects; modifying via one reference affects all aliases.

7. Chained comparisons evaluate left-to-right with short-circuit semantics. '1 < 2 < 3' is True, but be careful with mixed comparison operators.

8. Dictionary mutation during iteration raises a RuntimeError. Copy keys to mutate safely: `for k in list(d.keys()):`.

9. Decorator wrapping: when tracing a decorated function, trace both the wrapper and the wrapped function. The wrapper runs first and may transform inputs or outputs.

10. Recursion base case mistakes: always mark base cases in your trace to avoid infinite recursive narration mistakes.

Practice these ten problems weekly. They cover the highest-return concepts interviewers test with short trace puzzles.

Related Video

A curated companion video with short authored timestamps and a concise summary.

Short timestamps & notes

[00:00] Intro & problem list

[01:20] Worked example walkthroughs

[03:30] Common pitfalls and tips

Summary: Short practice problems and worked solutions for common trace-table interview questions.

Practice what you just learned

Apply these concepts in PyCodeIt's interactive sandbox with real problems.

Start Python Practice

Related Python Tutorials