Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum Function
What is the output of this recursive function when called with n = 4?
DSA Python
def recursive_sum(n): if n == 0: return 0 else: return n + recursive_sum(n - 1) print(recursive_sum(4))
Attempts:
2 left
💡 Hint
Think about how the function adds numbers from n down to 0.
✗ Incorrect
The function adds n to the sum of all numbers below it until it reaches 0, which stops recursion. So, 4 + 3 + 2 + 1 + 0 = 10.
❓ Predict Output
intermediateOutput of Recursive Factorial Function
What is the output of this recursive factorial function when called with n = 3?
DSA Python
def factorial(n): if n == 1: return 1 else: return n * factorial(n - 1) print(factorial(3))
Attempts:
2 left
💡 Hint
Factorial multiplies n by factorial of n-1 until it reaches 1.
✗ Incorrect
factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6.
🔧 Debug
advancedIdentify the Error in Recursive Function
What error does this recursive function produce when called with n = 2?
DSA Python
def countdown(n): if n == 0: print('Blastoff!') else: print(n) countdown(n) countdown(2)
Attempts:
2 left
💡 Hint
Check if the recursive call reduces the problem size.
✗ Incorrect
The function calls countdown(n) without reducing n, causing infinite recursion and a RecursionError.
❓ Predict Output
advancedOutput of Recursive Fibonacci Function
What is the output of this recursive Fibonacci function when called with n = 5?
DSA Python
def fibonacci(n): if n <= 1: return n else: return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(5))
Attempts:
2 left
💡 Hint
Fibonacci adds the two previous numbers starting from 0 and 1.
✗ Incorrect
fibonacci(5) = fibonacci(4) + fibonacci(3) = 3 + 2 = 5.
🧠 Conceptual
expertUnderstanding Base Case Importance
Why is the base case essential in a recursive function?
Attempts:
2 left
💡 Hint
Think about what happens if recursion never stops.
✗ Incorrect
The base case stops recursion by providing a condition to end recursive calls, preventing infinite loops.
