0
0
DSA Typescriptprogramming~20 mins

Recursion Concept and Call Stack Visualization in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 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 Simple Recursive Sum Function
What is the output of the following TypeScript code that uses recursion to sum numbers from n down to 1?
DSA Typescript
function recursiveSum(n: number): number {
  if (n <= 0) return 0;
  return n + recursiveSum(n - 1);
}

console.log(recursiveSum(4));
A0
B4
C10
DUndefined (runtime error)
Attempts:
2 left
💡 Hint
Think about how the function adds n to the sum of all numbers below it.
Predict Output
intermediate
2:00remaining
Output of Recursive Factorial Function
What will be printed when this recursive factorial function is called with 3?
DSA Typescript
function factorial(n: number): number {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}

console.log(factorial(3));
A9
B6
C0
DError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint
Factorial multiplies n by factorial of n-1 until it reaches 0.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Fibonacci Function
What is the output of this TypeScript code when calling fibonacci(5)?
DSA Typescript
function fibonacci(n: number): number {
  if (n === 0) return 0;
  if (n === 1) return 1;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

console.log(fibonacci(5));
A5
B8
CStack overflow error
DTypeError: fibonacci is not a function
Attempts:
2 left
💡 Hint
Trace fibonacci(5) calls and sum results.
Predict Output
advanced
2:00remaining
Output of Recursive Function with Multiple Calls
What is the output of this recursive function that prints numbers during the call and return phases?
DSA Typescript
function printNumbers(n: number): void {
  if (n <= 0) return;
  console.log('Call:', n);
  printNumbers(n - 1);
  console.log('Return:', n);
}

printNumbers(3);
A"Call: 3" "Call: 2" "Call: 1" "Return: 1" "Return: 2" "Return: 3"
B"Return: 1" "Return: 2" "Return: 3" "Call: 3" "Call: 2" "Call: 1"
C"Call: 1" "Call: 2" "Call: 3" "Return: 3" "Return: 2" "Return: 1"
D"Call: 3" "Return: 3" "Call: 2" "Return: 2" "Call: 1" "Return: 1"
Attempts:
2 left
💡 Hint
Notice the order of console.log before and after the recursive call.
🧠 Conceptual
expert
2:00remaining
Maximum Call Stack Depth in Recursive Function
Consider this recursive function without a base case. What will happen when calling infiniteRecursion(1)?
DSA Typescript
function infiniteRecursion(n: number): number {
  return infiniteRecursion(n + 1) + 1;
}

infiniteRecursion(1);
AReturns Infinity
BReturns 1
CRuns forever without error
DThrows RangeError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint
Think about what happens when recursion never stops.