0
0
DSA Typescriptprogramming~20 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA Typescript - Challenge Your Understanding

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 Recursive Factorial Function
What is the output of the following TypeScript code that uses recursion to calculate factorial?
DSA Typescript
function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}
console.log(factorial(4));
A120
B10
C24
D6
Attempts:
2 left
💡 Hint
Think about how factorial multiplies numbers from n down to 1.
🧠 Conceptual
intermediate
2:00remaining
Why Recursion Can Express Tree Traversals More Cleanly Than Loops
Why is recursion often preferred over loops when traversing tree structures?
ARecursion naturally handles branching by calling itself for each child, while loops require manual stack management.
BLoops are faster than recursion for trees, so recursion is avoided.
CRecursion cannot be used for trees because it causes infinite loops.
DLoops automatically traverse all nodes without extra code.
Attempts:
2 left
💡 Hint
Think about how trees have multiple branches and how recursion calls itself for each branch.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Fibonacci Implementation
What error does the following TypeScript code produce when run?
DSA Typescript
function fib(n: number): number {
  if (n === 0) return 0;
  if (n === 1) return 1;
  return fib(n - 1) + fib(n - 2);
}
console.log(fib(-1));
AStack overflow error due to infinite recursion
BReturns 0 without error
CReturns 1 without error
DSyntax error
Attempts:
2 left
💡 Hint
Consider what happens when n is negative in the recursive calls.
🚀 Application
advanced
2:00remaining
Using Recursion to Flatten Nested Arrays
Which option correctly outputs the flattened array from a nested array using recursion in TypeScript?
DSA Typescript
const nested = [1, [2, [3, 4], 5], 6];
function flatten(arr: any[]): number[] {
  let result: number[] = [];
  for (const el of arr) {
    if (Array.isArray(el)) {
      result = result.concat(flatten(el));
    } else {
      result.push(el);
    }
  }
  return result;
}
console.log(flatten(nested));
A[1, 2, [3, 4], 5, 6]
B[1, [2, [3, 4], 5], 6]
C[[1, 2, 3, 4, 5, 6]]
D[1, 2, 3, 4, 5, 6]
Attempts:
2 left
💡 Hint
Flattening means removing all nested arrays and putting all elements in one list.
🧠 Conceptual
expert
2:00remaining
Why Some Problems Cannot Be Cleanly Solved Using Loops Alone
Which statement best explains why recursion is necessary for some problems instead of just using loops?
ALoops are always slower and less memory efficient than recursion.
BSome problems have self-similar structures that require the function to call itself with smaller inputs, which loops cannot express naturally.
CLoops cannot be nested, so they cannot handle complex problems.
DRecursion uses less code but cannot solve problems loops can.
Attempts:
2 left
💡 Hint
Think about problems like tree traversals or divide-and-conquer algorithms.