Challenge - 5 Problems
Recursion vs Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive vs Iterative Factorial
What is the output of the following Go code that calculates factorial using recursion and iteration?
DSA Go
package main import "fmt" func factorialRecursive(n int) int { if n == 0 { return 1 } return n * factorialRecursive(n-1) } func factorialIterative(n int) int { result := 1 for i := 1; i <= n; i++ { result *= i } return result } func main() { fmt.Println(factorialRecursive(5)) fmt.Println(factorialIterative(5)) }
Attempts:
2 left
💡 Hint
Factorial of 5 is 5*4*3*2*1.
✗ Incorrect
Both recursive and iterative functions correctly compute factorial of 5 as 120.
🧠 Conceptual
intermediateWhen is recursion preferred over iteration?
Which scenario best explains when recursion is preferred over iteration?
Attempts:
2 left
💡 Hint
Think about problems that break down into smaller similar problems.
✗ Incorrect
Recursion is ideal for problems like tree traversal where the problem divides into similar subproblems.
❓ Predict Output
advancedStack Overflow Risk in Recursion
What happens when the following recursive function is called with a large input like 100000?
DSA Go
package main import "fmt" func countDown(n int) { if n == 0 { fmt.Println("Done") return } countDown(n - 1) } func main() { countDown(100000) }
Attempts:
2 left
💡 Hint
Consider how deep recursion affects memory.
✗ Incorrect
Deep recursion can exhaust the call stack causing a runtime stack overflow error.
🧠 Conceptual
advancedIteration Advantages Over Recursion
Which advantage is true for iteration compared to recursion?
Attempts:
2 left
💡 Hint
Think about how function calls use memory.
✗ Incorrect
Iteration uses a single loop and avoids the overhead of multiple function calls, saving memory.
🚀 Application
expertChoosing Between Recursion and Iteration for Fibonacci
Given the task to compute the 40th Fibonacci number, which approach is best and why?
Attempts:
2 left
💡 Hint
Consider performance and memory for large inputs.
✗ Incorrect
Simple recursion for Fibonacci is very slow and causes many repeated calls. Iteration computes efficiently without stack issues.
