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
Think about how factorial is defined recursively: n * factorial(n-1), with factorial(0) = 1.
✗ Incorrect
The function calls itself with decreasing values until it reaches 0, then multiplies back up: 4*3*2*1 = 24.
🧠 Conceptual
intermediateWhy Recursion Can Express Tree Traversal Better Than Loops
Why is recursion often preferred over loops for traversing tree structures?
Attempts:
2 left
💡 Hint
Think about how trees have multiple branches and how recursion calls itself for each branch.
✗ Incorrect
Recursion fits tree traversal naturally by visiting each node and recursively visiting children, avoiding manual stack use.
❓ Predict Output
advancedOutput of Recursive Fibonacci Function
What is the output of this Go code that calculates the 5th Fibonacci number 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
Recall the Fibonacci sequence: 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 elements of a slice recursively?
DSA Go
package main import "fmt" func sum(arr []int) int { if len(arr) == 0 { return 0 } return arr[0] + sum(arr[1:]) } func main() { fmt.Println(sum(nil)) }
Attempts:
2 left
💡 Hint
What happens when len(arr) == 0 and arr is nil?
✗ Incorrect
In Go, len(nil) for a slice is 0, so sum(nil) returns 0 safely without error.
🚀 Application
expertWhy Recursion is Needed for Backtracking Algorithms
Which statement best explains why recursion is essential for backtracking algorithms like solving mazes or puzzles?
Attempts:
2 left
💡 Hint
Think about how recursion manages state and returns to previous steps automatically.
✗ Incorrect
Recursion naturally saves the current state on the call stack and returns to it when a path fails, simplifying backtracking logic.
