0
0
DSA Typescriptprogramming~20 mins

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

Choose your learning style9 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.