Challenge - 5 Problems
DP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The function uses memoization to efficiently calculate Fibonacci numbers. fib(5) returns 5 because the sequence is 0,1,1,2,3,5...
🧠 Conceptual
intermediate1:30remaining
Identifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems?
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same smaller problems are solved multiple times.
✗ Incorrect
Computing Fibonacci numbers recursively without memoization recalculates the same values many times, showing overlapping subproblems.
🔧 Debug
advanced2: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));Attempts:
2 left
💡 Hint
Check how memoization cache is checked before recursive calls.
✗ Incorrect
fib(4) = fib(3) + fib(2) = 2 + 1 = 3. The code works correctly and outputs 3.
🚀 Application
advanced2: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.
Attempts:
2 left
💡 Hint
Try to break amount 6 into smaller amounts and combine their solutions.
✗ Incorrect
Minimum coins for 6 is 2: using coins 3 + 3 or 4 + 1 + 1 is more than 2 coins, so 3+3 is optimal.
🧠 Conceptual
expert3:00remaining
Why Dynamic Programming Requires Both Properties
Why does dynamic programming require both overlapping subproblems and optimal substructure properties?
Attempts:
2 left
💡 Hint
Think about how dynamic programming builds solutions efficiently.
✗ Incorrect
Dynamic programming works by solving smaller overlapping problems once and combining their optimal solutions to solve the bigger problem.