Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Memoization to Optimize Recursion in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Fibonacci with Memoization
What is the output of the following TypeScript code that calculates Fibonacci numbers using memoization?
DSA Typescript
function fib(n: number, memo: Record<number, number> = {}): number {
  if (n <= 1) return n;
  if (memo[n]) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(6));
A5
B13
C8
D21
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13...
❓ Predict Output
intermediate
2:00remaining
Memoization Cache Content After Execution
After running the following code, what is the content of the memo object?
DSA Typescript
function factorial(n: number, memo: Record<number, number> = {}): number {
  if (n <= 1) return 1;
  if (memo[n]) return memo[n];
  memo[n] = n * factorial(n - 1, memo);
  return memo[n];
}
const memo: Record<number, number> = {};
factorial(4, memo);
console.log(memo);
A{"4":24}
B{"2":2,"3":6,"4":24}
C{"3":6,"4":24}
D{"1":1,"2":2,"3":6,"4":24}
Attempts:
2 left
💡 Hint
Factorial builds up from 1 to n, memo stores intermediate results.
🔧 Debug
advanced
2:00remaining
Identify the Error in Memoized Fibonacci
What error will this TypeScript code produce when run?
DSA Typescript
function fib(n: number, memo: Record<number, number> = {}): number {
  if (n <= 1) return n;
  if (memo[n]) return memo[n];
  memo[n] = fib(n - 1) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(5));
AOutput: 5
BStack overflow due to infinite recursion
CTypeError: Cannot read property '5' of undefined
DOutput: 8
Attempts:
2 left
💡 Hint
Check how memo is passed in recursive calls.
❓ Predict Output
advanced
2:00remaining
Output of Memoized Climbing Stairs Problem
What is the output of this memoized climbing stairs function for n=4?
DSA Typescript
function climbStairs(n: number, memo: Record<number, number> = {}): number {
  if (n <= 2) return n;
  if (memo[n]) return memo[n];
  memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
  return memo[n];
}
console.log(climbStairs(4));
A3
B7
C6
D5
Attempts:
2 left
💡 Hint
Number of ways to climb 4 steps taking 1 or 2 steps at a time.
🧠 Conceptual
expert
2:00remaining
Why Memoization Improves Recursive Fibonacci
Which statement best explains why memoization improves the performance of the recursive Fibonacci function?
AIt stores previously computed results to avoid repeated calculations, reducing exponential time to linear time.
BIt changes the recursion to iteration, which is faster in all cases.
CIt parallelizes the recursive calls to run simultaneously on multiple processors.
DIt uses a stack data structure to store all recursive calls explicitly.
Attempts:
2 left
💡 Hint
Think about how many times each Fibonacci number is calculated without memoization.

Practice

(1/5)
1. What is the main purpose of using memoization in recursive functions?
easy
A. To avoid using any variables inside the function
B. To make the function run slower by adding extra steps
C. To convert recursion into iteration automatically
D. To store results of expensive function calls and reuse them later

Solution

  1. Step 1: Understand recursion inefficiency

    Recursive functions often repeat the same calculations multiple times, which wastes time.
  2. Step 2: Role of memoization

    Memoization saves the results of these repeated calculations in a storage (like an object) so they can be reused without recalculating.
  3. Final Answer:

    To store results of expensive function calls and reuse them later -> Option D
  4. Quick Check:

    Memoization = Store and reuse results [OK]
Hint: Memoization saves repeated results to speed up recursion [OK]
Common Mistakes:
  • Thinking memoization slows down the function
  • Confusing memoization with iteration
  • Believing memoization removes recursion
2. Which of the following is the correct way to declare a memo object in TypeScript for memoization?
easy
A. let memo = []
B. const memo: Record = {}
C. var memo = 0
D. const memo: string = ''

Solution

  1. Step 1: Identify memo storage type

    Memoization requires a storage that maps input (number) to output (number), so an object with number keys and number values is suitable.
  2. Step 2: Check TypeScript syntax

    Record correctly types an object with number keys and number values.
  3. Final Answer:

    const memo: Record = {} -> Option B
  4. Quick Check:

    Memo object type = Record [OK]
Hint: Use Record for memo object in TypeScript [OK]
Common Mistakes:
  • Using array instead of object for memo
  • Declaring memo as a number or string
  • Not initializing memo as an empty object
3. What will be the output of the following TypeScript code?
function fib(n: number, memo: Record = {}): number {
  if (n <= 1) return n;
  if (memo[n]) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(5));
medium
A. 5
B. 8
C. 3
D. 13

Solution

  1. Step 1: Understand Fibonacci sequence

    Fibonacci numbers start as 0,1,1,2,3,5,8,... The 5th Fibonacci number (0-based) is 5.
  2. Step 2: Trace fib(5) call

    fib(5) = fib(4) + fib(3) = 3 + 2 = 5, memoization avoids repeated calls.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    fib(5) = 5 [OK]
Hint: Remember Fibonacci sequence values for small n [OK]
Common Mistakes:
  • Confusing index starting at 0 or 1
  • Ignoring memoization effect on output
  • Calculating fib(5) as fib(6)
4. Identify the error in this memoized factorial function and choose the correct fix:
function factorial(n: number, memo: Record = {}): number {
  if (n === 0) return 1;
  if (memo[n]) return memo[n];
  memo[n] = n * factorial(n - 1, memo);
  return memo[n];
}
console.log(factorial(5));
medium
A. Remove memo parameter from function
B. Add base case for n === 1
C. Change if (memo[n]) to if (memo[n] !== undefined)
D. Initialize memo as an array instead of object

Solution

  1. Step 1: Understand memo check issue

    Checking if (memo[n]) fails when memo[n] is 0 or falsy, but factorial values are always positive, so 0 is not possible here, but better to check for undefined explicitly.
  2. Step 2: Fix condition to check existence properly

    Use if (memo[n] !== undefined) to correctly detect cached values.
  3. Final Answer:

    Change if (memo[n]) to if (memo[n] !== undefined) -> Option C
  4. Quick Check:

    Check memo existence with !== undefined [OK]
Hint: Check memo keys with !== undefined to avoid false negatives [OK]
Common Mistakes:
  • Using if(memo[n]) which fails for falsy cached values
  • Adding unnecessary base case for n=1
  • Removing memo parameter breaks memoization
5. You want to optimize a recursive function that calculates the number of ways to climb n stairs taking 1 or 2 steps at a time. Which memoization approach below correctly implements this in TypeScript?
hard
A. function climb(n: number, memo: Record = {}): number { if (n <= 1) return 1; if (memo[n] !== undefined) return memo[n]; memo[n] = climb(n - 1, memo) + climb(n - 2, memo); return memo[n]; }
B. function climb(n: number): number { if (n <= 1) return 1; return climb(n - 1) + climb(n - 2); }
C. function climb(n: number, memo: number[] = []): number { if (n <= 1) return 1; if (memo[n]) return memo[n]; memo[n] = climb(n - 1, memo) * climb(n - 2, memo); return memo[n]; }
D. function climb(n: number, memo: Record = {}): number { if (n === 0) return 0; if (memo[n]) return memo[n]; memo[n] = climb(n - 1, memo) + climb(n - 2, memo); return memo[n]; }

Solution

  1. Step 1: Understand problem base cases

    Number of ways to climb 0 or 1 stairs is 1 (one way: stand still or one step).
  2. Step 2: Check memoization and recursion logic

    function climb(n: number, memo: Record = {}): number { if (n <= 1) return 1; if (memo[n] !== undefined) return memo[n]; memo[n] = climb(n - 1, memo) + climb(n - 2, memo); return memo[n]; } correctly uses memo object, checks with !== undefined, and sums ways from n-1 and n-2 stairs.
  3. Step 3: Identify errors in other options

    function climb(n: number): number { if (n <= 1) return 1; return climb(n - 1) + climb(n - 2); } lacks memoization, function climb(n: number, memo: number[] = []): number { if (n <= 1) return 1; if (memo[n]) return memo[n]; memo[n] = climb(n - 1, memo) * climb(n - 2, memo); return memo[n]; } multiplies instead of sums, function climb(n: number, memo: Record = {}): number { if (n === 0) return 0; if (memo[n]) return memo[n]; memo[n] = climb(n - 1, memo) + climb(n - 2, memo); return memo[n]; } returns 0 for n=0 which is incorrect.
  4. Final Answer:

    Option A code correctly implements memoized climb function -> Option A
  5. Quick Check:

    Memo + sum of previous two = correct climb ways [OK]
Hint: Sum ways for n-1 and n-2 with memo check !== undefined [OK]
Common Mistakes:
  • Forgetting memoization causing slow recursion
  • Multiplying instead of adding ways
  • Wrong base case for n=0
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