Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a subset sum exists using backtracking.
DSA Javascript
function isSubsetSum(arr, n, sum) {
if (sum === 0) return true;
if (n === 0) return false;
if (arr[n - 1] > sum) {
return isSubsetSum(arr, n - 1, sum);
}
return isSubsetSum(arr, n - 1, sum) || isSubsetSum(arr, n - 1, sum [1] arr[n - 1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction in the recursive call.
Not handling the base cases correctly.
✗ Incorrect
We subtract the current element from sum to check if the remaining sum can be formed.
2fill in blank
mediumComplete the code to find the maximum number of activities that can be selected using a greedy approach.
DSA Javascript
function maxActivities(start, finish) {
let count = 1;
let lastFinish = finish[0];
for (let i = 1; i < finish.length; i++) {
if (start[i] [1] lastFinish) {
count++;
lastFinish = finish[i];
}
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < causes overlapping activities to be selected.
Using > or >= reverses the logic.
✗ Incorrect
We select the next activity only if it starts after the last selected activity finishes.
3fill in blank
hardFix the error in the backtracking function to generate all subsets of an array.
DSA Javascript
function generateSubsets(arr, index, current, result) {
if (index === arr.length) {
result.push(current.slice());
return;
}
generateSubsets(arr, index + 1, current, result);
current.push(arr[index]);
generateSubsets(arr, index + 1, current, result);
current.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using push instead of pop causes infinite recursion.
Using shift or unshift removes elements from the wrong end.
✗ Incorrect
We remove the last added element to backtrack and explore other subsets.
4fill in blank
hardFill both blanks to implement a backtracking solution for the N-Queens problem.
DSA Javascript
function isSafe(board, row, col, n) {
for (let i = 0; i < col; i++) {
if (board[row][i] === 1) return false;
}
for (let i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] === 1) return false;
}
for (let i = row, j = col; i < n && j >= 0; i++, j--) {
if (board[i][j] === 1) return false;
}
return true;
}
function solveNQueens(board, col, n) {
if (col === n) return true;
for (let i = 0; i < n; i++) {
if (isSafe(board, i, col, n)) {
board[i][col] = 1;
if (solveNQueens(board, col [1] 1, n)) return true;
board[i][col] = [2];
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition for column increment.
Setting the board cell to null instead of 0.
✗ Incorrect
We move to the next column by adding 1, and reset the board cell to 0 when backtracking.
5fill in blank
hardFill all three blanks to implement a greedy algorithm for the fractional knapsack problem.
DSA Javascript
function fractionalKnapsack(values, weights, capacity) {
let n = values.length;
let ratio = [];
for (let i = 0; i < n; i++) {
ratio.push({ index: i, value: values[i] [1] weights[i] });
}
ratio.sort((a, b) => b.value - a.value);
let totalValue = 0;
for (let i = 0; i < n; i++) {
let idx = ratio[i].index;
if (weights[idx] <= capacity) {
capacity -= weights[idx];
totalValue += values[idx];
} else {
totalValue += values[idx] [2] capacity [3] weights[idx];
break;
}
}
return totalValue;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for ratio.
Using addition or subtraction instead of multiplication for fractional value.
✗ Incorrect
We calculate value-to-weight ratio by division, multiply fraction of capacity by value, and divide capacity by weight for fraction.
