Bird
Raised Fist0
DSA C++programming~20 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA C++ - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Recursive vs Loop Factorial
What is the output of the following C++ code that calculates factorial using recursion and a loop?
DSA C++
#include <iostream>

int factorial_recursive(int n) {
    if (n <= 1) return 1;
    return n * factorial_recursive(n - 1);
}

int factorial_loop(int n) {
    int result = 1;
    for (int i = 2; i <= n; ++i) {
        result *= i;
    }
    return result;
}

int main() {
    std::cout << factorial_recursive(5) << " " << factorial_loop(5) << std::endl;
    return 0;
}
A24 120
B120 120
C120 24
D120 720
Attempts:
2 left
💡 Hint
Both functions calculate factorial of 5, but one uses recursion and the other uses a loop.
🧠 Conceptual
intermediate
1:30remaining
Why Recursion is Needed for Tree Traversal
Why is recursion often preferred over loops for traversing tree data structures?
ABecause trees have hierarchical structures that loops cannot easily represent without extra data structures
BBecause loops are slower than recursion in all cases
CBecause recursion uses less memory than loops
DBecause loops cannot be used with pointers
Attempts:
2 left
💡 Hint
Think about how trees branch and how loops handle repeated tasks.
🔧 Debug
advanced
2:00remaining
Identify the Output of Recursive Fibonacci with Memoization
What is the output of this C++ code that calculates Fibonacci numbers using recursion with memoization?
DSA C++
#include <iostream>
#include <vector>

int fib(int n, std::vector<int>& memo) {
    if (n <= 1) return n;
    if (memo[n] != -1) return memo[n];
    memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
    return memo[n];
}

int main() {
    int n = 6;
    std::vector<int> memo(n + 1, -1);
    std::cout << fib(n, memo) << std::endl;
    return 0;
}
ASyntaxError
B13
C5
D8
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
❓ Predict Output
advanced
2:00remaining
Output of Recursive Function with Multiple Recursive Calls
What is the output of this C++ code that prints numbers using recursion with two recursive calls?
DSA C++
#include <iostream>

void printNumbers(int n) {
    if (n == 0) return;
    std::cout << n << " ";
    printNumbers(n - 1);
    printNumbers(n - 1);
}

int main() {
    printNumbers(3);
    return 0;
}
A3 2 1 1 1 2 1
B3 2 1 2 1 1 1
C3 2 1 1 2 1 1
D3 2 1 1 1 1 1
Attempts:
2 left
💡 Hint
Trace the calls carefully: each call prints n, then calls itself twice with n-1.
🧠 Conceptual
expert
2:30remaining
Why Some Problems Cannot Be Solved Cleanly with Loops Alone
Which of the following best explains why some problems require recursion instead of just loops?
ABecause recursion can naturally handle problems with self-similar subproblems and variable depth, which loops cannot express without complex state management
BBecause loops are always slower and less memory efficient than recursion
CBecause recursion is the only way to repeat code in programming
DBecause loops cannot be nested inside other loops
Attempts:
2 left
💡 Hint
Think about problems like parsing nested structures or exploring trees.

Practice

(1/5)
1. Why do we use recursion instead of loops in some problems?
easy
A. Because recursion can express problems with self-similar structure more naturally
B. Because loops are slower than recursion in all cases
C. Because recursion uses less memory than loops always
D. Because loops cannot repeat any actions

Solution

  1. Step 1: Understand problem structure

    Some problems have parts that look like smaller versions of themselves, like trees or nested lists.
  2. Step 2: Compare recursion and loops

    Loops repeat actions linearly, but recursion can call itself to handle nested or self-similar parts easily.
  3. Final Answer:

    Because recursion can express problems with self-similar structure more naturally -> Option A
  4. Quick Check:

    Self-similar problems naturally use recursion [OK]
Hint: Recursion fits nested/self-similar problems better [OK]
Common Mistakes:
  • Thinking loops are always slower
  • Assuming recursion always uses less memory
  • Believing loops cannot repeat actions
2. Which of the following is the correct syntax to define a recursive function in C++?
easy
A. void func() { func(); }
B. void func() { if(condition) func(); else return; }
C. void func() { for(int i=0; i<10; i++) func(); }
D. void func() { while(true) func(); }

Solution

  1. Step 1: Identify base case in recursion

    Recursive functions must have a condition to stop calling themselves to avoid infinite loops.
  2. Step 2: Check options for base case

    void func() { if(condition) func(); else return; } has an if condition that stops recursion; others either lack base case or use loops incorrectly.
  3. Final Answer:

    void func() { if(condition) func(); else return; } -> Option B
  4. Quick Check:

    Recursive functions must have base case [OK]
Hint: Look for base case to stop recursion [OK]
Common Mistakes:
  • Missing base case causing infinite recursion
  • Confusing loops with recursion calls
  • Using infinite loops inside recursive functions
3. What is the output of the following C++ code?
#include <iostream>
int factorial(int n) {
  if (n == 0) return 1;
  else return n * factorial(n - 1);
}

int main() {
  std::cout << factorial(4);
  return 0;
}
medium
A. 16
B. 10
C. 120
D. 24

Solution

  1. Step 1: Understand factorial recursion

    factorial(4) = 4 * factorial(3), factorial(3) = 3 * factorial(2), and so on until factorial(0) = 1.
  2. Step 2: Calculate factorial(4)

    factorial(4) = 4 * 3 * 2 * 1 = 24.
  3. Final Answer:

    24 -> Option D
  4. Quick Check:

    4! = 24 [OK]
Hint: Recall factorial definition: n! = n*(n-1)! [OK]
Common Mistakes:
  • Confusing factorial with sum
  • Forgetting base case returns 1
  • Multiplying incorrectly
4. Find the error in this recursive function that tries to sum numbers from n down to 1:
int sum(int n) {
  if (n == 0) return 0;
  else return n + sum(n);
}
medium
A. Recursive call uses sum(n) instead of sum(n - 1)
B. Missing base case for n == 1
C. Function should return n * sum(n - 1)
D. No error, function works correctly

Solution

  1. Step 1: Check recursive call argument

    The function calls sum(n) again without decreasing n, causing infinite recursion.
  2. Step 2: Correct recursive call

    It should call sum(n - 1) to reduce n each time and reach base case.
  3. Final Answer:

    Recursive call uses sum(n) instead of sum(n - 1) -> Option A
  4. Quick Check:

    Recursion requires decreasing argument [OK]
Hint: Recursive call must reduce problem size [OK]
Common Mistakes:
  • Calling recursion with same argument causing infinite loop
  • Confusing sum with product
  • Missing base case
5. You want to print all paths from root to leaves in a tree structure. Why is recursion better than loops for this task?
hard
A. Because loops cannot access tree nodes
B. Because recursion uses less CPU than loops
C. Because loops cannot handle multiple branching paths easily
D. Because recursion always runs faster than loops

Solution

  1. Step 1: Understand tree traversal

    Printing all root-to-leaf paths requires exploring multiple branches at each node.
  2. Step 2: Compare recursion and loops for branching

    Loops handle linear repetition well but struggle with multiple branches; recursion naturally explores each branch by calling itself.
  3. Final Answer:

    Because loops cannot handle multiple branching paths easily -> Option C
  4. Quick Check:

    Recursion naturally handles branching [OK]
Hint: Recursion handles branching paths naturally [OK]
Common Mistakes:
  • Thinking loops can easily replace recursion for trees
  • Assuming recursion is always faster
  • Believing loops cannot access nodes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard