Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate Fibonacci number using simple recursion.
DSA Javascript
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) [1] fib(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the sum.
✗ Incorrect
The Fibonacci number is the sum of the two previous Fibonacci numbers, so we add fib(n - 1) and fib(n - 2).
2fill in blank
mediumComplete the code to implement memoization for Fibonacci to avoid repeated calculations.
DSA Javascript
function fibMemo(n, memo = {}) {
if (n <= 1) return n;
if (memo[n]) return memo[n];
memo[n] = fibMemo(n - 1, memo) [1] fibMemo(n - 2, memo);
return memo[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Not storing the result in memo.
Using wrong operators instead of addition.
✗ Incorrect
Memoization stores results of fib(n - 1) and fib(n - 2) and adds them to avoid repeated work.
3fill in blank
hardFix the error in the greedy approach to select maximum number of activities that don't overlap.
DSA Javascript
function maxActivities(activities) {
activities.sort((a, b) => a.end [1] b.end);
let count = 1;
let lastEnd = activities[0].end;
for (let i = 1; i < activities.length; i++) {
if (activities[i].start >= lastEnd) {
count++;
lastEnd = activities[i].end;
}
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition in sort comparator causing wrong order.
Sorting by start time instead of end time.
✗ Incorrect
To sort activities by their end time ascending, we subtract b.end from a.end.
4fill in blank
hardFill in the blank to create a dynamic programming table for the 0/1 Knapsack problem.
DSA Javascript
function knapsack(weights, values, W) {
const n = weights.length;
const dp = Array(n + 1).fill(null).map(() => Array(W + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let w = 1; w <= W; w++) {
if (weights[i - 1] [1] w) {
dp[i][w] = Math.max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing incorrect table filling.
Mixing up indices in dp array.
✗ Incorrect
We check if the current item's weight is less than or equal to the current capacity (w) to decide inclusion. The Math.max compares excluding and including the item.
5fill in blank
hardFill all three blanks to implement a greedy algorithm for coin change problem (minimum coins).
DSA Javascript
function minCoins(coins, amount) {
coins.sort((a, b) => b [1] a);
let count = 0;
let i = 0;
while (amount > 0 && i < coins.length) {
if (coins[i] [2] amount) {
count += Math.floor(amount [3] coins[i]);
amount = amount % coins[i];
}
i++;
}
return amount === 0 ? count : -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting coins ascending instead of descending.
Using wrong comparison operators in if condition.
Using multiplication instead of division to count coins.
✗ Incorrect
Sort coins descending (b - a). Check if coin value is less than or equal to amount. Use division (/) to find how many coins fit.
