0
0
DSA Typescriptprogramming~20 mins

Overlapping Subproblems and Optimal Substructure in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fibonacci with Memoization
What is the output of the following TypeScript code that calculates Fibonacci numbers using memoization?
DSA Typescript
function fib(n: number, memo: {[key: number]: number} = {}): number {
  if (n <= 1) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(5));
A13
B5
C8
D3
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
🧠 Conceptual
intermediate
1:30remaining
Identifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems?
AComputing Fibonacci numbers using recursion without memoization
BFinding the shortest path in a graph using Dijkstra's algorithm
CSorting an array using quicksort
DCalculating factorial of a number using recursion
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same smaller problems are solved multiple times.
🔧 Debug
advanced
2:00remaining
Fix the Memoization Bug
The following TypeScript code tries to use memoization for Fibonacci but produces wrong output. What is the output when calling fib(4)?
DSA Typescript
function fib(n: number, memo: {[key: number]: number} = {}): number {
  if (n <= 1) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(4));
A4
B5
Cundefined
D3
Attempts:
2 left
💡 Hint
Check how memoization cache is checked before recursive calls.
🚀 Application
advanced
2:30remaining
Optimal Substructure in Coin Change Problem
Given unlimited coins of denominations [1, 3, 4], what is the minimum number of coins needed to make amount 6? Use the optimal substructure property.
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Try to break amount 6 into smaller amounts and combine their solutions.
🧠 Conceptual
expert
3:00remaining
Why Dynamic Programming Requires Both Properties
Why does dynamic programming require both overlapping subproblems and optimal substructure properties?
ABecause overlapping subproblems allow reuse of solutions and optimal substructure ensures global solution from local solutions
BBecause overlapping subproblems guarantee no recursion and optimal substructure guarantees sorting
CBecause overlapping subproblems mean problems are independent and optimal substructure means problems are unrelated
DBecause overlapping subproblems allow parallel processing and optimal substructure allows random access
Attempts:
2 left
💡 Hint
Think about how dynamic programming builds solutions efficiently.