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.

MediumTechnical
43 practiced

Given a deeply nested dictionary such as {'a': {'b': 1, 'c': {'d': 2}}, 'e': 3}, return the dotted key paths to every leaf (['a.b', 'a.c.d', 'e']) as a generator. The nesting can be very deep, so make sure the solution does not hit a recursion depth limit.

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.

That is every published Recursion and Backtracking question for Data Engineer so far. Browse the other topics in this category, or practice this one interactively.