0
0
DSA Typescriptprogramming~10 mins

Recursion Concept and Call Stack Visualization in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the function call itself recursively.

DSA Typescript
function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial([1]);
}
Drag options to blanks, or click blank then click option'
An + 1
Bn - 1
C1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n directly does not reduce the problem size.
2fill in blank
medium

Complete the code to correctly calculate the sum of numbers from 1 to n using recursion.

DSA Typescript
function sumToN(n: number): number {
  if (n === 1) return 1;
  return n + sumToN([1]);
}
Drag options to blanks, or click blank then click option'
An - 1
B1
Cn + 1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n directly does not reduce the problem size.
3fill in blank
hard

Fix the error in the recursive Fibonacci function to avoid infinite recursion.

DSA Typescript
function fibonacci(n: number): number {
  if (n <= 1) return n;
  return fibonacci([1]) + fibonacci(n - 2);
}
Drag options to blanks, or click blank then click option'
An - 1
B1
Cn - 2
Dn + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using the same argument twice does not reduce the problem.
4fill in blank
hard

Fill both blanks to create a recursive function that prints numbers from n down to 1.

DSA Typescript
function printDown(n: number): void {
  if (n === 0) return;
  console.log([1]);
  printDown([2]);
}
Drag options to blanks, or click blank then click option'
An
Bn - 1
Cn + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Printing n - 1 before calling printDown causes off-by-one errors.
Calling printDown with n + 1 causes infinite recursion.
5fill in blank
hard

Fill both blanks to create a recursive function that builds an array of numbers from 1 to n.

DSA Typescript
function buildArray(n: number): number[] {
  if (n === 0) return [];
  const arr = buildArray([1]);
  arr.push([2]);
  return arr;
}
Drag options to blanks, or click blank then click option'
An - 1
Bn
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing 1 every time instead of n causes incorrect arrays.
Calling buildArray with n + 1 causes infinite recursion.