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 C# code that calculates factorial using recursion and iteration?
DSA C#
using System; class Program { static int RecursiveFactorial(int n) { if (n <= 1) return 1; return n * RecursiveFactorial(n - 1); } static int IterativeFactorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } static void Main() { Console.WriteLine(RecursiveFactorial(5)); Console.WriteLine(IterativeFactorial(5)); } }
Attempts:
2 left
💡 Hint
Recall factorial of 5 is 5*4*3*2*1.
✗ Incorrect
Both recursive and iterative methods 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 like exploring branches or nested structures.
✗ Incorrect
Recursion is ideal for problems like tree traversal where the problem breaks down into similar subproblems.
❓ Predict Output
advancedStack Overflow in Deep Recursion
What happens when the following recursive function is called with a large input like 100000?
DSA C#
using System; class Program { static int RecursiveSum(int n) { if (n == 0) return 0; return n + RecursiveSum(n - 1); } static void Main() { Console.WriteLine(RecursiveSum(100000)); } }
Attempts:
2 left
💡 Hint
Consider the maximum call stack size in C#.
✗ Incorrect
Deep recursion like 100000 calls exceeds the call stack limit causing StackOverflowException.
🧠 Conceptual
advancedIteration advantage over recursion
Why is iteration often preferred over recursion in performance-critical applications?
Attempts:
2 left
💡 Hint
Think about what happens during each function call in recursion.
✗ Incorrect
Iteration avoids the overhead of multiple function calls and uses constant memory, making it faster and more memory efficient.
🚀 Application
expertChoosing recursion or iteration for Fibonacci sequence
Given the task to compute the 30th Fibonacci number, which approach is best and why?
Attempts:
2 left
💡 Hint
Consider time complexity and stack limits.
✗ Incorrect
Naive recursion for Fibonacci is exponential and slow; iteration computes efficiently without stack issues.
