0
0
DSA Typescriptprogramming~20 mins

Factorial Using Recursion in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factorial Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this factorial function call?
Consider the following TypeScript code that calculates factorial using recursion. What will be printed when calling factorial(4)?
DSA Typescript
function factorial(n: number): number {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

console.log(factorial(4));
A24
B10
C120
D6
Attempts:
2 left
💡 Hint
Remember factorial of n is n * factorial of (n-1), and factorial of 0 is 1.
Predict Output
intermediate
2:00remaining
What is the output of factorial(0)?
Using the same factorial function, what will be printed when calling factorial(0)?
DSA Typescript
function factorial(n: number): number {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

console.log(factorial(0));
AError
B0
Cundefined
D1
Attempts:
2 left
💡 Hint
Factorial of zero is defined as 1.
🔧 Debug
advanced
2:00remaining
What error does this factorial function cause?
Examine this factorial function. What error will occur when calling factorial(0)?
DSA Typescript
function factorial(n: number): number {
  if (n === 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

console.log(factorial(0));
AReturns 6
BReturns 0
CStack overflow error
DSyntax error
Attempts:
2 left
💡 Hint
Check the base case condition and what happens when n reaches 0.
🧠 Conceptual
advanced
2:00remaining
How many times is factorial called when factorial(5) is executed?
Using the standard recursive factorial function, how many total calls to factorial are made when calculating factorial(5)?
DSA Typescript
function factorial(n: number): number {
  if (n === 0) {
    return 1;
  }
  return n * factorial(n - 1);
}

factorial(5);
A5
B6
C4
D1
Attempts:
2 left
💡 Hint
Count the initial call plus all recursive calls until base case.
🚀 Application
expert
2:00remaining
What is the output of this modified factorial function?
This factorial function prints the current value of n before the recursive call. What is the output when calling factorial(3)?
DSA Typescript
function factorial(n: number): number {
  if (n === 0) {
    return 1;
  }
  console.log(n);
  return n * factorial(n - 1);
}

factorial(3);
A[3, 2, 1]
B[1, 2, 3]
C[3, 2, 1, 0]
D[0, 1, 2, 3]
Attempts:
2 left
💡 Hint
The function prints before calling itself with n-1.