Challenge - 5 Problems
Master of Overlapping Subproblems and Optimal Substructure
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci with Memoization
What is the output of the following Python code that uses memoization to compute Fibonacci numbers?
DSA Python
def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] print(fib(5))
Attempts:
2 left
💡 Hint
Remember the Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The Fibonacci sequence is 0,1,1,2,3,5,8,... fib(5) is 5.
🧠 Conceptual
intermediateIdentifying 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 solves the same subproblems repeatedly, showing overlapping subproblems.
❓ Predict Output
advancedOutput of Dynamic Programming Table for Coin Change
What is the final value of dp[5] after running this code that counts ways to make change for 5 using coins [1,2,5]?
DSA Python
coins = [1, 2, 5] dp = [0] * 6 dp[0] = 1 for coin in coins: for x in range(coin, 6): dp[x] += dp[x - coin] print(dp[5])
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.
✗ Incorrect
Ways to make 5: {1+1+1+1+1}, {1+1+1+2}, {1+2+2}, {5} total 4 ways.
🔧 Debug
advancedIdentify the Error in Memoized Fibonacci
What error does this code raise when calling fib(3)?
DSA Python
def fib(n, memo={}): if n <= 1: return n if n in memo: return memo[n] memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] print(fib(3))
Attempts:
2 left
💡 Hint
Check the order of base case and memo check.
✗ Incorrect
The code runs correctly and outputs fib(3) = 2 without error.
🧠 Conceptual
expertOptimal Substructure in Shortest Path Problem
Which statement best describes the optimal substructure property in shortest path problems?
Attempts:
2 left
💡 Hint
Optimal substructure means optimal solutions contain optimal subsolutions.
✗ Incorrect
In shortest path, any part of the path is itself a shortest path between its endpoints.
