Recursion and Backtracking Questions
Recursive decomposition, base/recursive-case design, and backtracking search over combinatorial spaces (permutations, subsets, constraint satisfaction, N-queens style problems). Covers recursion-tree reasoning, pruning, and converting recursion to iteration. The conceptual bridge into dynamic programming and search.
Write a recursive function in Python that returns the power set (all subsets) of a given list of unique integers. Include an example for [1,2,3] and explain the time and space complexity. Discuss how you'd adapt this in an ML preprocessing job when input size can be large (n up to 30).
Describe a technique to detect and prune symmetric states in a recursive search by canonicalizing state representations or hashing isomorphisms. Provide an example such as board rotations for puzzles, explain how to build a canonical form, and analyze memory overhead versus pruning benefit.
Write a recursive Python function powerset(nums: List[int]) -> List[List[int]] that returns all subsets of a list of unique integers. Ensure no duplicates, describe time and space complexity, and show how your implementation avoids shared mutable-state bugs when building partial subsets. Order of subsets does not matter.
Design a backtracking-based Sudoku solver that accepts a 9x9 grid with zeros for blanks and fills it to a valid solution. Explain candidate generation for each cell, a heuristic to pick the next cell (e.g., minimum remaining values), and a pruning strategy. Discuss time/space tradeoffs when using constraint propagation versus naive backtracking.
Explain how recursion works in a typical language runtime: define base case and recursive case, describe call stack behavior and stack frames, and show how local variables and return values propagate. Use factorial(4) as an example and write the call-return sequence showing frames and values.
Unlock Full Question Bank
Get access to all Recursion and Backtracking interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.