0
0
DSA Typescriptprogramming~20 mins

DP vs Recursion vs Greedy Choosing the Right Tool in DSA Typescript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DP vs Recursion vs Greedy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Fibonacci Function
What is the output of the following TypeScript code that uses recursion to calculate Fibonacci numbers?
DSA Typescript
function fib(n: number): number {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
console.log(fib(5));
A3
B8
C13
D5
Attempts:
2 left
💡 Hint
Trace the recursive calls for fib(5).
🧠 Conceptual
intermediate
1:30remaining
Choosing Greedy vs Dynamic Programming
Which problem characteristic best indicates that a greedy algorithm is the right choice over dynamic programming?
AOptimal substructure without overlapping subproblems
BNo optimal substructure
COptimal substructure and overlapping subproblems
DOverlapping subproblems without optimal substructure
Attempts:
2 left
💡 Hint
Greedy algorithms work when local choices lead to global optimum.
🔧 Debug
advanced
2:00remaining
Why Does This Memoized Recursive Code Fail?
Consider this TypeScript code for memoized recursion to compute factorial. What error does it produce?
DSA Typescript
function factorial(n: number, memo: {[key: number]: number} = {}): number {
  if (n <= 1) return 1;
  if (memo[n]) return memo[n];
  memo[n] = n * factorial(n - 1, memo);
  return memo[n];
}
console.log(factorial(0));
AReturns 1
BStack overflow error
CTypeError: Cannot read property of undefined
DReturns undefined
Attempts:
2 left
💡 Hint
Check base case for n=0.
Predict Output
advanced
2:00remaining
Output of Greedy Coin Change Algorithm
What is the output of this TypeScript code that uses a greedy approach to make change for 30 cents with coins [25, 10, 5, 1]?
DSA Typescript
function greedyChange(coins: number[], amount: number): number[] {
  const result: number[] = [];
  for (const coin of coins) {
    while (amount >= coin) {
      amount -= coin;
      result.push(coin);
    }
  }
  return result;
}
console.log(greedyChange([25, 10, 5, 1], 30));
A[5, 5, 5, 5, 5, 5]
B[25, 5]
C[25, 1, 1, 1, 1, 1]
D[10, 10, 10]
Attempts:
2 left
💡 Hint
Greedy picks largest coin first.
🧠 Conceptual
expert
2:30remaining
When Is Dynamic Programming Necessary Over Greedy?
Which scenario best explains when dynamic programming is necessary instead of a greedy approach?
AWhen local optimal choices always lead to global optimum
BWhen the problem has optimal substructure but no overlapping subproblems
CWhen the problem has optimal substructure and overlapping subproblems requiring exploration of all subproblems
DWhen the problem has no optimal substructure
Attempts:
2 left
💡 Hint
Dynamic programming solves overlapping subproblems efficiently.