Challenge - 5 Problems
Fibonacci Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci Recursive Call
What is the output of the following Go code that prints Fibonacci numbers recursively for n=5?
DSA Go
package main import "fmt" func fib(n int) int { if n <= 1 { return n } return fib(n-1) + fib(n-2) } func main() { for i := 0; i < 5; i++ { fmt.Print(fib(i), " ") } }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0 and 1.
✗ Incorrect
The Fibonacci sequence starts with 0 and 1. The function returns n if n is 0 or 1. For n=5, the printed sequence is 0 1 1 2 3.
❓ Predict Output
intermediateValue of fib(6) Using Recursion
What is the value returned by fib(6) in the following Go recursive Fibonacci function?
DSA Go
func fib(n int) int { if n <= 1 { return n } return fib(n-1) + fib(n-2) }
Attempts:
2 left
💡 Hint
Calculate fib(6) by adding fib(5) and fib(4).
✗ Incorrect
fib(6) = fib(5) + fib(4) = 5 + 3 = 8.
🔧 Debug
advancedIdentify the Runtime Error in Fibonacci Code
What error will the following Go code produce when calling fib(-1)?
DSA Go
func fib(n int) int { if n <= 1 { return n } return fib(n-1) + fib(n-2) } func main() { fmt.Println(fib(-1)) }
Attempts:
2 left
💡 Hint
Check the base case condition for negative input.
✗ Incorrect
For n = -1, the base case n <= 1 returns n (-1), so no infinite recursion occurs. But since fib(-1) returns -1, no error occurs. However, the function is not designed for negative inputs, so it returns -1 safely.
❓ Predict Output
advancedOutput of Fibonacci Sequence with Memoization
What is the output of this Go program that uses memoization to print Fibonacci numbers up to n=7?
DSA Go
package main import "fmt" var memo = map[int]int{} func fib(n int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fib(n-1) + fib(n-2) return memo[n] } func main() { for i := 0; i <= 7; i++ { fmt.Print(fib(i), " ") } }
Attempts:
2 left
💡 Hint
Memoization stores previously calculated results to avoid repeated work.
✗ Incorrect
The memoization technique caches results, so fib(n) returns the correct Fibonacci number. The sequence printed is 0 1 1 2 3 5 8 13.
🧠 Conceptual
expertTime Complexity of Recursive Fibonacci
What is the time complexity of the naive recursive Fibonacci function fib(n) without memoization?
Attempts:
2 left
💡 Hint
Consider how many calls fib(n) makes to fib(n-1) and fib(n-2).
✗ Incorrect
The naive recursive Fibonacci function calls itself twice for each n > 1, leading to exponential growth in calls, so time complexity is O(2^n).
