Challenge - 5 Problems
Fibonacci Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Call
What is the output of the following code snippet?
DSA Python
def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) print(fib(5))
Attempts:
2 left
💡 Hint
Remember the Fibonacci sequence starts 0, 1, 1, 2, 3, 5...
✗ Incorrect
fib(5) returns the 5th Fibonacci number which is 5 (0-based index: fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5).
❓ Predict Output
intermediateOutput of Recursive Fibonacci with Print
What will be printed when this code runs?
DSA Python
def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) for i in range(4): print(fib(i), end=' -> ') print('null')
Attempts:
2 left
💡 Hint
Check the Fibonacci values for 0, 1, 2, 3
✗ Incorrect
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, so the printed sequence is 0 -> 1 -> 1 -> 2 -> null.
🔧 Debug
advancedIdentify the Error in Recursive Fibonacci
What error will this code produce when run?
DSA Python
def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-3) print(fib(4))
Attempts:
2 left
💡 Hint
Check the recursive calls and base cases carefully.
✗ Incorrect
The function calls fib(n-3) but does not handle n < 0, causing infinite recursion and eventually a RecursionError.
❓ Predict Output
advancedOutput of Memoized Fibonacci Recursive Function
What is the output of this memoized Fibonacci function?
DSA Python
def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: memo[n] = n else: memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] print(fib(6))
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated work.
✗ Incorrect
fib(6) is 8 in the Fibonacci sequence starting with 0,1.
🧠 Conceptual
expertTime Complexity of Recursive Fibonacci
What is the time complexity of the naive recursive Fibonacci function without memoization?
Attempts:
2 left
💡 Hint
Consider how many calls are made for each fib(n).
✗ Incorrect
Each call to fib(n) makes two calls to fib(n-1) and fib(n-2), leading to exponential growth.
