LearnFrenzy
Sign in Take a free test

DSA & Algorithms · Topic 11 of 12

Dynamic Programming: questions with answers

Memoisation and tabulation, 1D problems (stairs, robber, coins, LIS), 2D tables (grid paths, LCS, edit distance, knapsack), interval DP and state design.

Questions
45
Timed sets
3
Answers run or computed
35
Your accuracy
–

What this topic covers

18 subtopics
  • 1D DP5
  • Coin change5
  • Foundations4
  • Knapsack4
  • String DP4
  • 2D DP3
  • Interval DP3
  • Subsequences3
  • LCS2
  • Memoisation2
  • State machine DP2
  • Tabulation2
Show all 18 subtopics
  • Bitmask DP1
  • Classic DP1
  • Edit distance1
  • Space optimisation1
  • Subset sum1
  • Unbounded knapsack1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Dynamic programming solves a problem by combining answers to overlapping subproblems, computing each one only once. The hard part is the state – what does dp[i] (or dp[i][j]) mean? – and the transition that builds it from smaller states. Top-down (memoised recursion) and bottom-up (tables) give the same answers. Many questions here show a partly filled table with one cell marked "?"; every value was computed by a checker program, often against a brute-force solution.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Foundations Beginner Practice

Which two properties make a problem suitable for dynamic programming?

Optimal substructure and overlapping subproblems
Sorted input and unique values
A greedy choice and no subproblems
Recursion and randomness
Show answer and explanation
Correct answer A

Explanation

Optimal substructure: the best answer is built from best answers to smaller problems. Overlapping subproblems: the same smaller problems recur, so storing them pays off. Merge sort has the first but not the second, so it is plain divide and conquer.

Why the other options are wrong

  • BInput order is irrelevant.
  • CThat describes greedy algorithms.
  • DRandomness is unrelated.
Question 2 LCS Intermediate Practice Answer computed

What is the length of the longest common subsequence of ABCBDAB and BDCABA?

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 4

Explanation

For example BCBA or BDAB. dp[i][j] = dp[i − 1][j − 1] + 1 if the characters match, else max(dp[i − 1][j], dp[i][j − 1]) – O(mn).

Question 3 Interval DP Advanced Practice Answer computed

Matrices A (10 × 30), B (30 × 5) and C (5 × 60) are multiplied. What is the minimum number of scalar multiplications?

The answer is a number – work it out, then open the solution.

Show answer and explanation
Correct answer 4500

Explanation

(AB)C costs 10·30·5 + 10·5·60 = 1500 + 3000 = 4500; A(BC) costs 30·5·60 + 10·30·60 = 27,000. Matrix-chain DP tries every split point k for every interval: O(n³).

42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DSA & Algorithms

All 12 topics

Topics, levels and full mocks
  1. 01 Time and Space Complexity45 questions · 3 levels
  2. 02 Arrays and Strings45 questions · 3 levels
  3. 03 Linked Lists45 questions · 3 levels
  4. 04 Stacks and Queues45 questions · 3 levels
  5. 05 Hashing45 questions · 3 levels
  6. 06 Recursion and Backtracking45 questions · 3 levels
  7. 07 Sorting and Searching45 questions · 3 levels
  8. 08 Trees and BST45 questions · 3 levels
  9. 09 Heaps and Priority Queues45 questions · 3 levels
  10. 10 Graphs45 questions · 3 levels
  11. 11 Dynamic Programming45 questions · 3 levels
  12. 12 Greedy and Bit Manipulation45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links