Dynamic Programming And Memoization
Asked of: Software Engineer
Last updated

What's being tested
These problems test dynamic programming: defining a state, deriving a recurrence, choosing correct base cases, and evaluating overlapping subproblems efficiently. You must distinguish ordered versus unordered counting, unlimited versus single-use choices, and state dimensions such as position, capacity, or transaction count.
Patterns & templates
- One-dimensional counting DP:
dp[sum] += dp[sum - value]; iterate candidate values inside the sum loop when order should not matter. - For ordered sequences, iterate totals first and choices second;
dp[target] = Σ dp[target - x], withdp[0] = 1. - Subsequence counting uses
dp[i][j]for source/target prefixes: match contributes take-or-skip; timeO(mn), space reducible toO(n). - 0/1 knapsack uses take-or-skip; iterate capacity downward in a one-dimensional table to prevent reusing an item.
- Memoization caches recursive states only when first visited; typically changes exponential recursion to
O(number of states × transitions). - Stock trading can use constant-size state compression: track cash/hold after each of two completed transactions, handling buy-before-sell ordering.
- Use 64-bit counts where results can exceed 32-bit limits; define unreachable states carefully and state whether modulo arithmetic is required.
Common pitfalls
Pitfall: Reversing loop order changes whether permutations count separately or whether each item can be reused.
Pitfall: Setting
dp[0] = 0breaks counting recurrences; the empty sequence is usually one valid way to form sum zero.
Pitfall: Claiming memoization is always
O(n)without identifying the state dimensions, transition count, and memory usage.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Count ordered ways to reach a target sum with unlimited reuse of distinct numbersGeneral · Software Engineer · Other · medium
- Maximum loot from a binary tree of houses when a parent and its child cannot both be robbedGeneral · Software Engineer · Other · medium
- What is the time complexity of a recursive algorithm to compute the Fibonacci sequence?General · Software Engineer · Other · easy
- Minimum Cost to Paint a Row of Houses With Three Colors and No Two Neighbors MatchingGeneral · Software Engineer · Other · medium
- Explain the concept of memoization in dynamic programming.General · Software Engineer · Other · easy
- Find the maximum stock profit with at most two buy and sell transactionsGeneral · Software Engineer · Other · hard
- Count the distinct subsequences of a string that equal a target stringGeneral · Software Engineer · Other · hard
- Maximize the value of items packed under a weight limit, each item taken at most onceGeneral · Software Engineer · Other · medium
Related concepts
- Dynamic Programming And MemoizationCoding & Algorithms
- Dynamic Programming, Backtracking, And Combinatorial SearchCoding & Algorithms
- Dynamic Programming, Greedy, And Combinatorial OptimizationCoding & Algorithms
- Dynamic Programming And Mutable Range QueriesCoding & Algorithms
- Dynamic Programming, Backtracking, and State-Space SearchCoding & Algorithms
- Dynamic Programming, Scheduling, And Set CoverCoding & Algorithms