Fibonacci Using Recursion in DSA Go - Time & Space Complexity
We want to understand how the time to calculate Fibonacci numbers grows as the input number increases.
Specifically, how many steps does the recursive method take when finding the nth Fibonacci number?
Analyze the time complexity of the following code snippet.
func Fibonacci(n int) int {
if n <= 1 {
return n
}
return Fibonacci(n-1) + Fibonacci(n-2)
}
This code calculates the nth Fibonacci number by calling itself twice for smaller values until it reaches the base cases.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two recursive calls per function call.
- How many times: Each call generates two more calls until reaching base cases, causing many repeated calculations.
Each number requires calculating two smaller Fibonacci numbers, which themselves require two smaller numbers, and so on.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 177 calls |
| 20 | Over 21,000 calls |
| 30 | Over 2 million calls |
Pattern observation: The number of calls grows very fast, roughly doubling with each increase in n.
Time Complexity: O(2^n)
This means the time needed roughly doubles with each increase in n, making it very slow for large n.
[X] Wrong: "Since each call does only two calls, the time is just twice n, or O(n)."
[OK] Correct: Each call creates two more calls, which themselves create two more, causing an exponential explosion, not a simple linear increase.
Understanding this recursive pattern helps you recognize when recursion leads to repeated work and why optimization or different methods are needed.
"What if we store previously calculated Fibonacci numbers in a list (memoization)? How would the time complexity change?"
Practice
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.Step 2: Relate to recursion
Recursion calls the function for the two previous numbers and adds their results.Final Answer:
Each number is the sum of the two preceding numbers -> Option CQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing difference with sum
- Assuming Fibonacci squares previous number
Solution
Step 1: Identify parameter and return types
The Fibonacci function takes an integer input and returns an integer output.Step 2: Match correct Go syntax
func fibonacci(n int) int matches the correct signature: func fibonacci(n int) int.Final Answer:
func fibonacci(n int) int -> Option AQuick Check:
Input and output are integers [OK]
- Using string or float types incorrectly
- Omitting function parameters
- Wrong return type
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
func main() {
fmt.Println(fibonacci(5))
}Solution
Step 1: Trace fibonacci(5) calls
fibonacci(5) = fibonacci(4) + fibonacci(3), fibonacci(4) = 3, fibonacci(3) = 2, so total = 5.Step 2: Calculate final value
Sum of fibonacci(4) and fibonacci(3) is 3 + 2 = 5.Final Answer:
5 -> Option AQuick Check:
Fibonacci(5) = 5 [OK]
- Confusing index starting at 0 or 1
- Adding wrong recursive results
- Miscounting base cases
func fibonacci(n int) int {
if n == 0 {
return 0
} else if n == 1 {
return 1
}
return fibonacci(n-1) - fibonacci(n-2)
}Solution
Step 1: Check recursive return operation
The Fibonacci sequence sums previous two numbers, but code subtracts them.Step 2: Correct the operation
Replace '-' with '+' to correctly add fibonacci(n-1) and fibonacci(n-2).Final Answer:
Change '-' to '+' in the return statement -> Option DQuick Check:
Fibonacci sums previous two numbers [OK]
- Using subtraction instead of addition
- Changing base cases incorrectly
- Removing necessary conditions
Solution
Step 1: Understand repeated calculations problem
Recursive Fibonacci recalculates same values many times, causing inefficiency.Step 2: Apply memoization technique
Memoization stores results of previous calls to reuse them, reducing repeated work.Final Answer:
Use memoization to store and reuse results -> Option BQuick Check:
Memoization optimizes recursion by caching [OK]
- Increasing recursion depth does not fix inefficiency
- Counting calls doesn't optimize calculation
- Random numbers unrelated to Fibonacci
Ready to prove it?
Pass the test and this pattern moves unknown
