Dynamic Programming, Backtracking, and State-Space Search
Asked of: Software Engineer
Last updated
What's being tested
This tests state modeling: turning coins, tiles, grids, intervals, or circular sequences into compact states with valid transitions. Interviewers look for correct DP recurrence, backtracking pruning, reachability reasoning, and clear complexity analysis before coding.
Patterns & templates
-
Top-down DFS + memoization —
dfs(state)returns best/possible outcome; cache tuples or encoded counts; usually reduces exponential search dramatically. -
Bottom-up DP recurrence — define
dp[i],dp[r][c], ordp[mask]; prove transition order and base cases before writing loops. -
Backtracking over multisets — use
counts[value], choose first nonzero item, try legal groups, undo mutations; avoids permuting equivalent arrangements. -
Reachability graph on implicit states — generate neighbors on demand instead of materializing graph; use
visitedfor feasibility,dpfor optimization. -
Circular array normalization — duplicate array/string or fix a cut point; handle wraparound carefully and validate parity/count feasibility first.
-
Space optimization — compress grid/path DP from
O(mn)toO(n)when transitions only depend on previous row or few prior states. -
Interval scheduling DP/greedy split — sort by end/start time, use binary search via
bisect_left; compareO(n log n)optimized versus simplerO(n^2).
Common pitfalls
Pitfall: Starting with recursion over raw permutations instead of canonical states causes duplicate work and timeouts on tile/multiset problems.
Pitfall: Forgetting base cases such as empty hand, zero coins, blocked start/end cell, or impossible odd counts leads to almost-correct solutions.
Pitfall: Giving only an algorithm name is not enough; state definition, transition, answer extraction, and complexity must be explicit.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- 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
- 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 max coins with 3-step token movesGoogle · Software Engineer · Take-home Project · easy
- Solve three coding interview problemsGoogle · Software Engineer · Onsite · medium
- Optimize 0/1 to bounded knapsack DPGoogle · Software Engineer · Onsite · Medium
- Find fair split of a two-color necklaceGoogle · Software Engineer · Onsite · Medium
- Maximize coins collected by 3-step piecesGoogle · Software Engineer · Onsite · medium
Related concepts
- Dynamic Programming, Backtracking, And Combinatorial SearchCoding & Algorithms
- Dynamic Programming, Scheduling, And Set CoverCoding & Algorithms
- Dynamic Programming And MemoizationCoding & Algorithms
- Graph Search, State Space, And Path OptimizationCoding & Algorithms
- Dynamic Programming And Mutable Range QueriesCoding & Algorithms
- Dynamic Programming And Combinatorial CountingCoding & Algorithms