Challenge - 5 Problems
Dynamic Programming Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Without Memoization
What is the output of the following code snippet when calling fib(5)?
DSA Python
def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) print(fib(5))
Attempts:
2 left
💡 Hint
Trace the calls fib(5) = fib(4) + fib(3) and so on.
✗ Incorrect
The recursive Fibonacci function returns the 5th Fibonacci number, which is 5.
❓ Predict Output
intermediateOutput of Recursive Fibonacci With Memoization
What is the output of the following code snippet when calling fib(5)?
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
Memoization stores results to avoid repeated calculations.
✗ Incorrect
Memoization optimizes recursion by storing results. fib(5) returns 5.
🧠 Conceptual
advancedWhy Pure Recursion Can Be Inefficient
Why can pure recursion without memoization be inefficient for problems like Fibonacci?
Attempts:
2 left
💡 Hint
Think about how many times fib(3) is calculated in fib(5) without memoization.
✗ Incorrect
Pure recursion repeats the same calculations many times, leading to slow performance.
🧠 Conceptual
advancedWhen to Use Dynamic Programming Instead of Recursion
In which situation is dynamic programming preferred over plain recursion?
Attempts:
2 left
💡 Hint
Dynamic programming stores results to reuse them efficiently.
✗ Incorrect
Dynamic programming is best when subproblems overlap and results can be reused to save time.
🔧 Debug
expertIdentify the Error in Recursive Fibonacci with Mutable Default Argument
What error or unexpected behavior occurs when running this code and why?
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)) print(fib(6))
Attempts:
2 left
💡 Hint
Consider what happens to the default argument memo between calls.
✗ Incorrect
Using a mutable default argument causes the memo dictionary to persist between calls, which can lead to unexpected behavior.
