What is dynamic programming?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Data Structures & Algorithms topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
Dynamic programming (DP) is an optimization technique for problems with overlapping subproblems and optimal substructure. It solves each subproblem once and stores the result (memoization or tabulation), avoiding redundant recomputation. Two approaches: (1) Top-down (Memoization): recursion + caching. Solve the problem recursively, but store computed results in a hash map or array. First call computes; subsequent calls return cached result. More intuitive, may have stack overhead; (2) Bottom-up (Tabulation): iterative, fill a table from base cases upward. No recursion overhead, typically more space-efficient. Steps to solve DP: (1) Identify if DP applies (overlapping subproblems, optimal substructure); (2) Define state clearly (what does dp[i] represent?); (3) Write the recurrence relation (dp[i] in terms of previous states); (4) Identify base cases; (5) Determine computation order. Classic problems: Fibonacci, 0/1 Knapsack, Longest Common Subsequence, Longest Increasing Subsequence, Coin Change, Edit Distance, Matrix Chain Multiplication, Rod Cutting. DP transforms exponential brute force into polynomial time.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Data Structures & Algorithms answers easy to follow.