DSAMaster Logo
DSAMaster
Dynamic Programming5 August 202625 min read

Top 20 Dynamic Programming Interview Questions and Answers (2026)

Master Dynamic Programming with top DP interview questions featuring detailed answers, step-by-step intuition, and complete code in C++, Java, Python, and JavaScript. From 1D DP to String DP and Knapsack.

D
Written by DSAMaster Team
DSAMaster Editorial

1. Introduction to Dynamic Programming

Dynamic Programming (DP) is a core topic in technical coding interviews at Google, Amazon, Microsoft, and Meta. Mastering DP boils down to recognizing recurring patterns: 1D DP, 2D Grid DP, Knapsack variants, and String Alignment.

Below are top Dynamic Programming questions with complete, production-grade solutions in C++, Java, Python, and JavaScript.


2. Core DP Questions

Q1. Climbing Stairs

Question: You are climbing a staircase with n steps. Each time you can climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Intuition: dp[i] = dp[i-1] + dp[i-2]. To reach step i, you must come from i-1 or i-2.

javascript
function climbStairs(n) { if (n <= 2) return n; let a = 1, b = 2; for (let i = 3; i <= n; i++) { let temp = a + b; a = b; b = temp; } return b; }

Time Complexity: O(n) | Space Complexity: O(1)


Q2. Coin Change (Minimum Coins)

Question: Return the fewest number of coins that you need to make up amount amount. If impossible, return -1.

javascript
function coinChange(coins, amount) { let dp = new Array(amount + 1).fill(Infinity); dp[0] = 0; for (let i = 1; i <= amount; i++) { for (let c of coins) { if (i >= c) dp[i] = Math.min(dp[i], 1 + dp[i - c]); } } return dp[amount] !== Infinity ? dp[amount] : -1; }

Time Complexity: O(amount x len(coins)) | Space Complexity: O(amount)


Q3. Longest Increasing Subsequence (LIS)

Question: Find the length of the longest strictly increasing subsequence in an integer array nums.

javascript
function lengthOfLIS(nums) { if (!nums || nums.length === 0) return 0; let n = nums.length; let dp = new Array(n).fill(1); let maxLen = 1; for (let i = 1; i < n; i++) { for (let j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], 1 + dp[j]); } } maxLen = Math.max(maxLen, dp[i]); } return maxLen; }

Time Complexity: O(n^2) | Space Complexity: O(n)


Q4. 0/1 Knapsack Problem

Question: Given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack.

javascript
function knapsack(W, wt, val, n) { let dp = Array.from({ length: n + 1 }, () => new Array(W + 1).fill(0)); for (let i = 1; i <= n; i++) { for (let w = 1; w <= W; w++) { if (wt[i - 1] <= w) { dp[i][w] = Math.max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]); } else { dp[i][w] = dp[i - 1][w]; } } } return dp[n][W]; }

Time Complexity: O(n x W) | Space Complexity: O(n x W)


Q5. House Robber

Question: You are a robber planning to rob houses along a street. Adjacent houses have security systems connected, so you cannot rob two adjacent houses. Return the maximum money you can rob.

Intuition: At house i, you either rob house i (and take nums[i] + dp[i-2]) or skip it (and take dp[i-1]). State relation: dp[i] = max(dp[i-1], nums[i] + dp[i-2]).

javascript
function rob(nums) { let prev1 = 0, prev2 = 0; for (let num of nums) { let temp = Math.max(prev1, prev2 + num); prev2 = prev1; prev1 = temp; } return prev1; }

Time Complexity: O(n) | Space Complexity: O(1)


Q6. Longest Common Subsequence (LCS)

Question: Given two strings text1 and text2, return the length of their longest common subsequence.

javascript
function longestCommonSubsequence(text1, text2) { let m = text1.length, n = text2.length; let dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0)); for (let i = 1; i <= m; i++) { for (let j = 1; j <= n; j++) { if (text1[i - 1] === text2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } return dp[m][n]; }

Time Complexity: O(m x n) | Space Complexity: O(m x n)


Q7. Edit Distance (Levenshtein Distance)

Question: Given two strings word1 and word2, return the minimum number of operations (insert, delete, replace) required to convert word1 into word2.

javascript
function minDistance(word1, word2) { let m = word1.length, n = word2.length; let dp = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0)); for (let i = 0; i <= m; i++) dp[i][0] = i; for (let j = 0; j <= n; j++) dp[0][j] = j; for (let i = 1; i <= m; i++) { for (let j = 1; j <= n; j++) { if (word1[i - 1] === word2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; else dp[i][j] = 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]); } } return dp[m][n]; }

Time Complexity: O(m x n) | Space Complexity: O(m x n)


3. Summary Table

ProblemPatternTimeSpace
Climbing Stairs1D DP (Fibonacci)O(n)O(1)
Coin ChangeUnbounded KnapsackO(amount x n)O(amount)
LISSubsequence DPO(n^2)O(n)
0/1 Knapsack2D Subset DPO(n x W)O(n x W)
House Robber1D Non-adjacent ChoiceO(n)O(1)
LCS2D String AlignmentO(m x n)O(m x n)
Edit Distance2D String TransformationO(m x n)O(m x n)

Practice all DP problems on DSAMaster's practice platform.