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.

HardTechnical
46 practiced

Convert a deeply recursive backtracking algorithm into a trampolined form to avoid native stack growth in a language without tail-call optimization. Show a trampoline runner, how recursive calls become thunks, and the trade-offs against an explicit stack.

HardTechnical
40 practiced

Implement an N-Queens counter that uses symmetry to cut the search, for n up to 15. Explain how board reflections reduce the work and how you avoid double counting for the center column when n is odd.

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
43 practiced

What is the difference between plain DFS and backtracking? Give an example where DFS is used for traversal and one where backtracking constructs and undoes partial solutions, and discuss how cycle handling differs.

MediumTechnical
42 practiced

Given a string s, return all possible palindrome partitionings of s using recursion and backtracking. Then speed up the palindrome checks with a precomputed table and explain the time and space trade-off against checking on the fly.

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.