0
0
DSA Typescriptprogramming~20 mins

Memoization Top Down DP 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 uses memoization to compute Fibonacci numbers?
DSA Typescript
function fib(n: number, memo: Record<number, number> = {}): number {
  if (n <= 1) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(6));
A13
B21
C5
D8
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13...
Predict Output
intermediate
2:00remaining
Output of Climbing Stairs Problem with Memoization
What is the output of this TypeScript code that counts ways to climb stairs using memoization?
DSA Typescript
function climbStairs(n: number, memo: Record<number, number> = {}): number {
  if (n <= 2) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
  return memo[n];
}
console.log(climbStairs(5));
A5
B13
C8
D10
Attempts:
2 left
💡 Hint
Think of ways to reach step 5 by 1 or 2 steps from previous steps.
🔧 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] !== undefined) return memo[n];
  memo[n] = fib(n - 1, memo) + fib(n - 2, memo);
  return memo[n];
}
console.log(fib(-1));
AStack overflow error
BReturns -1
CReturns 0
DReturns undefined
Attempts:
2 left
💡 Hint
What happens if input is negative and base case only handles n <= 1?
🧠 Conceptual
advanced
2:00remaining
Memoization Cache Size for Staircase Problem
If you run the memoized climbStairs function for n=10, how many entries will be stored in the memo cache after the call completes?
DSA Typescript
function climbStairs(n: number, memo: Record<number, number> = {}): number {
  if (n <= 2) return n;
  if (memo[n] !== undefined) return memo[n];
  memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
  return memo[n];
}
climbStairs(10);
A8
B9
C10
D11
Attempts:
2 left
💡 Hint
Memo stores results for each unique n from 3 to 10.
🚀 Application
expert
3:00remaining
Output of Memoized Coin Change Ways Count
What is the output of this TypeScript code that counts ways to make change using memoization?
DSA Typescript
function countWays(coins: number[], amount: number, index = 0, memo: Record<string, number> = {}): number {
  const key = amount + ',' + index;
  if (amount === 0) return 1;
  if (amount < 0 || index === coins.length) return 0;
  if (memo[key] !== undefined) return memo[key];
  memo[key] = countWays(coins, amount - coins[index], index, memo) + countWays(coins, amount, index + 1, memo);
  return memo[key];
}
console.log(countWays([1, 2, 5], 5));
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 5.