📖 Scenario: You are helping a friend understand how to calculate Fibonacci numbers using a simple function that calls itself. Fibonacci numbers are a famous sequence where each number is the sum of the two before it, starting with 0 and 1.
🎯 Goal: Build a Go program that uses a recursive function to find the Fibonacci number at a given position.
📋 What You'll Learn
Create a variable called n with the value 7
Create a recursive function called fibonacci that takes an integer num and returns an integer
Use the base cases where num is 0 or 1 to return num
For other cases, return the sum of fibonacci(num-1) and fibonacci(num-2)
Print the Fibonacci number for n using the fibonacci function
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve problems that break down into smaller similar problems.
💼 Career
Recursion is a fundamental concept in programming interviews and algorithm design. Knowing how to implement recursive functions is useful for software development and problem-solving.
Progress0 / 4 steps
1
Create the input variable
Create a variable called n and set it to 7.
DSA Go
Hint
Use n := 7 inside the main function to create the variable.
2
Define the recursive Fibonacci function
Define a function called fibonacci that takes an integer num and returns an integer. Use base cases to return num if it is 0 or 1.
DSA Go
Hint
Use if num == 0 || num == 1 { return num } inside the fibonacci function.
3
Add the recursive call
In the fibonacci function, return the sum of fibonacci(num-1) and fibonacci(num-2) for other cases.
DSA Go
Hint
Use return fibonacci(num-1) + fibonacci(num-2) to call the function recursively.
4
Print the Fibonacci number
Print the Fibonacci number for n using fmt.Println(fibonacci(n)) inside the main function.
DSA Go
Hint
Use fmt.Println(fibonacci(n)) to display the result.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the product of the two preceding numbers
B. Each number is the difference of the two preceding numbers
C. Each number is the sum of the two preceding numbers
D. Each number is the square of the previous number
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 C
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Remember Fibonacci adds two previous numbers [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing difference with sum
Assuming Fibonacci squares previous number
2. Which of the following is the correct Go function signature for a recursive Fibonacci function?
easy
A. func fibonacci(n int) int
B. func fibonacci(n string) string
C. func fibonacci(n float64) float64
D. func fibonacci() int
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 A
Quick Check:
Input and output are integers [OK]
Hint: Fibonacci uses int input and int output [OK]
Common Mistakes:
Using string or float types incorrectly
Omitting function parameters
Wrong return type
3. What is the output of the following Go code?
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
func main() {
fmt.Println(fibonacci(5))
}
medium
A. 5
B. 8
C. 3
D. 13
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 A
Quick Check:
Fibonacci(5) = 5 [OK]
Hint: Recall Fibonacci(5) = 5 from sequence 0,1,1,2,3,5 [OK]
Common Mistakes:
Confusing index starting at 0 or 1
Adding wrong recursive results
Miscounting base cases
4. Identify the error in this recursive Fibonacci function and select the fix:
func fibonacci(n int) int {
if n == 0 {
return 0
} else if n == 1 {
return 1
}
return fibonacci(n-1) - fibonacci(n-2)
}
medium
A. Add a loop instead of recursion
B. Change base case n == 0 to n == 2
C. Remove else if condition
D. Change '-' to '+' in the return statement
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 D
Quick Check:
Fibonacci sums previous two numbers [OK]
Hint: Fibonacci adds, never subtracts previous two numbers [OK]
Common Mistakes:
Using subtraction instead of addition
Changing base cases incorrectly
Removing necessary conditions
5. How can you optimize the recursive Fibonacci function to avoid repeated calculations?
hard
A. Increase the recursion depth limit
B. Use memoization to store and reuse results
C. Use a global variable to count calls
D. Replace recursion with a random number generator
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 B
Quick Check:
Memoization optimizes recursion by caching [OK]
Hint: Cache results to avoid repeated recursion calls [OK]
Common Mistakes:
Increasing recursion depth does not fix inefficiency
Counting calls doesn't optimize calculation
Random numbers unrelated to Fibonacci
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.