Dynamic Programming, Backtracking, And Combinatorial Search
Asked of: Software Engineer
Last updated
What's being tested
These problems test state-space modeling, dynamic programming, and backtracking over constrained combinatorial choices. Interviewers look for whether you can convert messy rules into compact states, prune impossible branches, prove optimal substructure, and implement without exponential blowups where avoidable.
Patterns & templates
-
Net-state reduction — collapse inputs into balances, counts, or masks before search; smaller canonical state often changes brute force from impossible to tractable.
-
Backtracking with pruning via
dfs(i, state)— try valid choices, undo mutations, skip duplicates; worst-case exponential, but acceptable for smalln. -
Memoization with
@lru_cacheorMap<State, Ans>— cache tuple/count-vector/bitmask states; watch mutable arrays as cache keys. -
Bitmask DP for subsets — use
dp[mask]or recursivesolve(mask); typical complexityO(n * 2^n)orO(3^n)depending transitions. -
Multiset partitioning — represent tiles/items as frequency counts; recursively remove groups, restore counts, and terminate when all counts are zero.
-
Minimax / candidate filtering — for feedback-driven guessing, maintain feasible candidates and choose guesses minimizing worst-case remaining set.
-
Kadane-style DP on arrays — track best subarray ending here, prefix minima/maxima, or boundary-conditioned states in
O(n)time.
Common pitfalls
Pitfall: Starting with raw permutations instead of compressed counts or balances creates factorial search and usually times out.
Pitfall: Forgetting to restore mutated state after a recursive call causes silent corruption across branches.
Pitfall: Caching by non-canonical state, such as unsorted balances, misses equivalent subproblems and destroys memoization effectiveness.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Maximize Boundary-Difference Subarray SumGoogle · Software Engineer · Onsite · medium
- Solve Two Array Optimization ProblemsGoogle · Software Engineer · Onsite · medium
- Determine if a 14-tile hand is winningGoogle · Software Engineer · Technical Screen · medium
- Maximize coins collected by tokens jumping +3Google · Software Engineer · Take-home Project · none
- Find secret word with match-count feedbackGoogle · Software Engineer · Technical Screen · hard
- Maximize coins with tokens moving by +3Google · Software Engineer · Take-home Project · easy
- Solve chat, grid paths, and car rentalsGoogle · Software Engineer · Technical Screen · medium
- Compute minimal transfers to settle group expensesGoogle · Software Engineer · Technical Screen · medium
- Minimize calls to find all bad test pairsGoogle · Software Engineer · Technical Screen · medium
- Optimize 0/1 to bounded knapsack DPGoogle · Software Engineer · Onsite · Medium
- Maximize coins collected by 3-step piecesGoogle · Software Engineer · Onsite · medium
- Solve three coding problemsGoogle · Software Engineer · Onsite · easy
Related concepts
- Dynamic Programming, Backtracking, and State-Space SearchCoding & Algorithms
- Dynamic Programming, Scheduling, And Set CoverCoding & Algorithms
- Dynamic Programming And MemoizationCoding & Algorithms
- Dynamic Programming And Combinatorial CountingCoding & Algorithms
- Dynamic Programming And Mutable Range QueriesCoding & Algorithms
- Graph Search, State Space, And Path OptimizationCoding & Algorithms