Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA Typescript - Test Your Knowledge

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

Complete the code to define a recursive function that counts down from n to 1.

DSA Typescript
function countdown(n: number): void {
  if (n <= 0) return;
  console.log(n);
  countdown([1]);
}
Drag options to blanks, or click blank then click option'
An - 1
Bn + 1
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n does not change the argument, causing infinite recursion.
2fill in blank
medium

Complete the code to calculate factorial of n 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
B1
Cn + 1
Dn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Returning n without recursion misses the factorial calculation.
3fill in blank
hard

Fix the error in the recursive Fibonacci function to correctly compute fib(n).

DSA Typescript
function fib(n: number): number {
  if (n <= 1) return n;
  return fib([1]) + fib(n - 2);
}
Drag options to blanks, or click blank then click option'
An
Bn - 1
Cn + 1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or 1 as argument does not compute Fibonacci correctly.
4fill in blank
hard

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

DSA Typescript
function sumTo(n: number): number {
  if (n === 1) return 1;
  return n [1] sumTo(n [2] 1);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition.
Not decreasing n causes infinite recursion.
5fill in blank
hard

Fill all three blanks to create a recursive function that reverses a string.

DSA Typescript
function reverse(str: string): string {
  if (str.length <= 1) return str;
  return reverse(str[1]) [2] str[3];
}
Drag options to blanks, or click blank then click option'
A.slice(1)
B+
C[0]
D.slice(0, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect slice indices.
Using multiplication instead of concatenation.

Practice

(1/5)
1. Why do we use recursion instead of loops in some problems?
easy
A. Because recursion can express problems with self-similar subproblems naturally
B. Because loops are slower than recursion in all cases
C. Because recursion uses less memory than loops always
D. Because loops cannot run more than 10 times

Solution

  1. Step 1: Understand recursion's nature

    Recursion breaks a problem into smaller versions of itself, which loops cannot express naturally.
  2. Step 2: Compare loops and recursion

    Loops repeat fixed steps but cannot easily handle problems defined by self-similar subproblems like tree traversals.
  3. Final Answer:

    Because recursion can express problems with self-similar subproblems naturally -> Option A
  4. Quick Check:

    Recursion = self-similar problem solving [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
  • Thinking loops are always faster
  • Believing recursion always uses less memory
  • Assuming loops cannot run many times
2. Which of the following is the correct syntax for a recursive function in TypeScript?
easy
A. function factorial(n: number): number { if (n == 0) return 0; else return n + factorial(n + 1); }
B. function factorial(n: number): number { while (n > 1) n = n - 1; return n; }
C. function factorial(n: number): number { for (let i = 1; i <= n; i++) return i; }
D. function factorial(n: number): number { if (n <= 1) return 1; else return n * factorial(n - 1); }

Solution

  1. Step 1: Identify recursive call

    function factorial(n: number): number { if (n <= 1) return 1; else return n * factorial(n - 1); } calls factorial inside itself with n-1, which is recursion.
  2. Step 2: Check base case correctness

    function factorial(n: number): number { if (n <= 1) return 1; else return n * factorial(n - 1); } returns 1 when n <= 1, preventing infinite calls.
  3. Final Answer:

    function factorial(n: number): number { if (n <= 1) return 1; else return n * factorial(n - 1); } -> Option D
  4. Quick Check:

    Recursive call with base case = function factorial(n: number): number { if (n <= 1) return 1; else return n * factorial(n - 1); } [OK]
Hint: Look for function calling itself with smaller input [OK]
Common Mistakes:
  • Missing base case causing infinite recursion
  • Using loops instead of recursive calls
  • Incorrect base case values
3. What is the output of this TypeScript code?
function printNumbers(n: number): void {
  if (n === 0) return;
  printNumbers(n - 1);
  console.log(n);
}
printNumbers(3);
medium
A. 3 2 1
B. 1 2 3
C. 3 2 1 0
D. 0 1 2 3

Solution

  1. Step 1: Trace recursive calls

    printNumbers(3) calls printNumbers(2), which calls printNumbers(1), then printNumbers(0) returns immediately.
  2. Step 2: Understand console.log order

    After reaching base case, console.log prints n while unwinding: prints 1, then 2, then 3.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Recursion unwinds printing ascending numbers [OK]
Hint: Recursion prints after call, so output is ascending [OK]
Common Mistakes:
  • Assuming print before recursive call prints descending
  • Including 0 in output incorrectly
  • Confusing order of prints
4. Identify the error in this recursive function and choose the fix:
function sumToN(n: number): number {
  return n + sumToN(n - 1);
}
medium
A. Add a base case to stop recursion when n <= 0
B. Change return to console.log to print values
C. Replace recursion with a for loop
D. Remove the parameter n

Solution

  1. Step 1: Identify missing base case

    The function calls itself without stopping condition, causing infinite recursion.
  2. Step 2: Add base case to stop recursion

    Adding if (n <= 0) return 0; stops recursion and returns correct sum.
  3. Final Answer:

    Add a base case to stop recursion when n <= 0 -> Option A
  4. Quick Check:

    Base case prevents infinite recursion [OK]
Hint: Always include base case to stop recursion [OK]
Common Mistakes:
  • Forgetting base case causes stack overflow
  • Changing return to console.log does not fix recursion
  • Removing parameter breaks logic
5. You want to flatten a nested array of numbers like [1, [2, [3, 4], 5], 6] into a single array [1, 2, 3, 4, 5, 6]. Why is recursion a better choice than loops here?
hard
A. Because recursion uses less memory than loops for arrays
B. Because loops can only flatten arrays with one level of nesting
C. Because recursion naturally handles unknown levels of nesting by calling itself
D. Because loops cannot access array elements

Solution

  1. Step 1: Understand nested structure

    The array has unknown depth of nested arrays, which loops alone cannot easily handle.
  2. Step 2: Recursion handles unknown depth

    Recursion calls itself on nested arrays, flattening all levels naturally without complex loops.
  3. Final Answer:

    Because recursion naturally handles unknown levels of nesting by calling itself -> Option C
  4. Quick Check:

    Recursion = natural for unknown nested depth [OK]
Hint: Recursion handles unknown nested depth easily [OK]
Common Mistakes:
  • Thinking loops can flatten any nested array easily
  • Believing recursion always uses less memory
  • Assuming loops cannot access array elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard