Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Simple Recursive Sum Function
What is the output of the following Python code that uses recursion to sum numbers from n down to 1?
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 adding numbers from 4 down to 1 step by step.
✗ Incorrect
The function adds 4 + 3 + 2 + 1 + 0 = 10. The base case returns 0 stopping recursion.
❓ Predict Output
intermediateOutput of Recursive Factorial Function
What is the output of this recursive factorial function when called with 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 of 3 is 3 * 2 * 1.
✗ Incorrect
factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6.
🔧 Debug
advancedIdentify the Error in Recursive Fibonacci Function
What error will this code produce when calling fibonacci(4)?
DSA Python
def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(4))
Attempts:
2 left
💡 Hint
Check the parentheses in the return statement.
✗ Incorrect
The return line was missing a closing parenthesis causing a syntax error. After fixing, the output is 3.
🧠 Conceptual
advancedCall Stack Depth in Recursive Function
If a recursive function calls itself with n-1 until n is 0, what is the maximum depth of the call stack when called with n=5?
Attempts:
2 left
💡 Hint
Count the initial call plus all recursive calls until base case.
✗ Incorrect
Calls happen for n=5,4,3,2,1,0 so total 6 calls on the stack at deepest point.
🚀 Application
expertOutput of Recursive String Reversal Function
What is the output of this recursive function that reverses a string when called with 'abc'?
DSA Python
def reverse_string(s): if s == '': return '' else: return reverse_string(s[1:]) + s[0] print(reverse_string('abc'))
Attempts:
2 left
💡 Hint
The function moves first character to the end recursively.
✗ Incorrect
The function calls itself on substring excluding first char and adds first char at end, reversing the string.
