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 function calls itself multiplying n by factorial of n-1 until n is 0, returning 1. So factorial(4) = 4*3*2*1 = 24.
❓ Predict Output
intermediateOutput of Recursive Fibonacci Function
What is the output of this Go code 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.
🔧 Debug
advancedIdentify the Error in Recursive Sum Function
What error does this Go code produce when trying to sum numbers recursively?
DSA Go
package main import "fmt" func sum(n int) int { if n == 0 { return 0 } return n + sum(n) } func main() { fmt.Println(sum(3)) }
Attempts:
2 left
💡 Hint
Check the recursive call argument.
✗ Incorrect
The function calls sum(n) instead of sum(n-1), causing infinite recursion and stack overflow.
🧠 Conceptual
advancedCall Stack Depth in Recursive Function
If a recursive function calls itself with n-1 until n is 0, what is the maximum call stack depth when called with n=7?
Attempts:
2 left
💡 Hint
Count the initial call plus each recursive call until base case.
✗ Incorrect
The call stack depth is n+1 because it includes the initial call plus calls down to 0.
🚀 Application
expertOutput of Recursive String Reverse Function
What is the output of this Go code that reverses a string recursively?
DSA Go
package main import ( "fmt" ) func reverse(s string) string { if len(s) == 0 { return "" } return reverse(s[1:]) + string(s[0]) } func main() { fmt.Println(reverse("go")) }
Attempts:
2 left
💡 Hint
The function moves first character to the end recursively.
✗ Incorrect
The function takes the first character and appends it after reversing the rest, so "go" becomes "og".
