Challenge - 5 Problems
Master of DP vs Recursion vs Greedy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Function
What is the output of the following JavaScript code that calculates Fibonacci numbers using recursion?
DSA Javascript
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(5));Attempts:
2 left
💡 Hint
Remember the Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
The function fib(5) returns the 5th Fibonacci number starting from 0 index: 0,1,1,2,3,5,8...
fib(5) = 5.
❓ Predict Output
intermediateOutput of Greedy Coin Change Algorithm
What is the output of the following JavaScript code that uses a greedy approach to make change for 27 cents with coins [25, 10, 5, 1]?
DSA Javascript
function greedyChange(amount, coins) {
let result = [];
for (let coin of coins) {
while (amount >= coin) {
amount -= coin;
result.push(coin);
}
}
return result;
}
console.log(greedyChange(27, [25, 10, 5, 1]));Attempts:
2 left
💡 Hint
Greedy picks the largest coin first as many times as possible.
✗ Incorrect
The greedy algorithm picks 25 first (27-25=2), then two 1-cent coins to make 27.
❓ Predict Output
advancedOutput of Memoized Fibonacci Function
What is the output of this JavaScript code that calculates Fibonacci numbers using memoization (dynamic programming)?
DSA Javascript
function fibMemo(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fibMemo(n - 1, memo) + fibMemo(n - 2, memo);
return memo[n];
}
console.log(fibMemo(10));Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
fibMemo(10) returns the 10th Fibonacci number (0-based index), which is 55.
❓ Predict Output
advancedOutput of Recursive Subset Sum Check
What is the output of this JavaScript code that checks if a subset of array sums to target using recursion?
DSA Javascript
function subsetSum(arr, target, index = 0) { if (target === 0) return true; if (index >= arr.length || target < 0) return false; return subsetSum(arr, target - arr[index], index + 1) || subsetSum(arr, target, index + 1); } console.log(subsetSum([3, 34, 4, 12, 5, 2], 9));
Attempts:
2 left
💡 Hint
Check if any subset sums exactly to 9.
✗ Incorrect
The subset [4, 5] sums to 9, so the function returns true.
🧠 Conceptual
expertChoosing the Right Tool for Optimization Problems
Which approach is best suited for solving optimization problems where overlapping subproblems exist and the problem exhibits optimal substructure?
Attempts:
2 left
💡 Hint
Think about storing results to avoid repeated work.
✗ Incorrect
Dynamic programming is ideal for problems with overlapping subproblems and optimal substructure because it stores intermediate results to avoid repeated calculations.
