Question presented to candidate: "Explain what backtracking is, how it differs from plain brute force, and show me the pattern on a small problem in Python."
What a strong answer should cover:
- The idea: build a solution one decision at a time, and undo the last decision when it cannot lead to an answer, then try the next option.
- The three-step pattern choose / explore / un-choose, and the base case that records a complete solution.
- Pruning: checking constraints before recursing, and why it is the difference between backtracking and brute force.
- The state-space tree picture, and complexity as (number of nodes) ร (work per node), usually exponential; space is the recursion depth.
- Python details: one shared
listas the path withappend/pop, recording a copy (path[:]or"".join(path)), and the default recursion limit of 1000 frames that caps the depth.
Code / implementation expected: Optional โ a short recursive generator in Python (for example generate parentheses) that shows choose / explore / un-choose and one pruning rule.