Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Factorial Function
What is the output of this Go code that calculates factorial using recursion?
DSA Go
package main import "fmt" func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } func main() { fmt.Println(factorial(4)) }
Attempts:
2 left
💡 Hint
Recall factorial of 4 is 4*3*2*1.
✗ Incorrect
The base case returns 1 when n is 0. The recursive case multiplies n by factorial of n-1. So factorial(4) = 4*3*2*1 = 24.
❓ Predict Output
intermediateOutput of Recursive Sum Function
What does this Go program print when summing numbers from n down to 1 recursively?
DSA Go
package main import "fmt" func sum(n int) int { if n <= 0 { return 0 } return n + sum(n-1) } func main() { fmt.Println(sum(3)) }
Attempts:
2 left
💡 Hint
Sum of 3 + 2 + 1 = ?
✗ Incorrect
The function adds n to sum(n-1) until n is 0 or less, then returns 0. So sum(3) = 3+2+1=6.
🔧 Debug
advancedIdentify the Infinite Recursion Bug
Which option shows a recursive function that will cause infinite recursion and eventually a stack overflow?
DSA Go
func countdown(n int) { if n == 0 { fmt.Println("Done") return } fmt.Println(n) countdown(n) }
Attempts:
2 left
💡 Hint
Check if the recursive call changes the argument to approach the base case.
✗ Incorrect
Option B calls countdown(n) without changing n, so it never reaches the base case and recurses infinitely.
🧠 Conceptual
advancedUnderstanding Base Case Importance
Why is the base case essential in a recursive function?
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
✗ Incorrect
The base case stops recursion by providing a condition to end recursive calls, preventing infinite loops.
❓ Predict Output
expertOutput of Recursive Fibonacci Function
What is the output of this Go program that calculates Fibonacci numbers recursively?
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() { fmt.Println(fib(5)) }
Attempts:
2 left
💡 Hint
Fibonacci sequence starts 0,1,1,2,3,5,...
✗ Incorrect
fib(5) = fib(4) + fib(3) = 3 + 2 = 5.
