Recursion and Backtracking Questions

Recursion mechanics (base and recursive cases, tracing call stack frames, what lives on the stack versus the heap, tail calls, recursion depth limits and stack overflow diagnosis, recursion-to-iteration conversion with an explicit stack or trampoline, recursion versus iteration trade-offs) and recursion on nested data and simple divide-and-conquer (Euclid's gcd, string reversal, flattening nested lists and dictionaries lazily with generators, recursive maximum subarray). Also backtracking search over combinatorial spaces: permutations (including duplicates and the k-th permutation), subsets, combinations, combination sum, generating balanced parentheses, N-Queens (with bitmasks and symmetry), Sudoku (with minimum-remaining-values ordering), word search, palindrome partitioning, Knight's Tour and Hamiltonian paths. Covers the choose/explore/undo template, how it differs from plain DFS, state-leakage bugs, pruning, and recursion-tree complexity (O(b^d)) including call-tree counts. Memoized recursion, tree traversals, and general graph search are covered elsewhere.

EasyTechnical
85 practiced

What does accidental state leakage mean in recursive and backtracking code? Show a concrete Python example (such as a shared list or mutable default argument) that is buggy, fix it, and describe tests that would catch this kind of bug.

MediumTechnical
42 practiced

Flatten an arbitrarily nested list of integers, such as [1, [2, [3, 4], 5], 6], into a generator that yields integers one at a time without building the whole flat list. Show the recursive version, then explain how you would do it iteratively with an explicit stack.

MediumTechnical
39 practiced

A backtracking search enumerates combinations that sum to a target and is too slow. Describe three pruning techniques that cut the explored space and, for each, how it preserves correctness and what it saves.

MediumTechnical
55 practiced

Implement a backtracking solver in Python that returns all valid N-Queens placements for a given N as lists of strings: def solve_n_queens(n: int) -> List[List[str]]. Explain how you check column and diagonal conflicts efficiently.

HardTechnical
43 practiced

Design and implement a solver for the Knight's Tour on an N x N board using backtracking. Describe how you choose the next move to keep branching low, where that approach can fail, and what you fall back to.

Unlock Full Question Bank

Get access to all 44 Recursion and Backtracking interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.