Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a subset sum equals target using backtracking.
DSA C#
bool SubsetSum(int[] nums, int target, int index) {
if (target == 0) return true;
if (index == nums.Length || target < 0) return false;
return SubsetSum(nums, target - nums[index], index + 1) || SubsetSum(nums, target, [1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index - 1 causes going backward which is incorrect.
Using 0 resets the index causing infinite recursion.
✗ Incorrect
We move to the next index without including the current number by increasing index by 1.
2fill in blank
mediumComplete the code to choose the next candidate in backtracking for the N-Queens problem.
DSA C#
bool IsSafe(int row, int col, int[] queens) {
for (int i = 0; i < row; i++) {
if (queens[i] == col || Math.Abs(queens[i] - col) == row - i) return false;
}
return true;
}
bool SolveNQueens(int row, int n, int[] queens) {
if (row == n) return true;
for (int col = 0; col < n; col++) {
if (IsSafe(row, col, queens)) {
queens[row] = col;
if (SolveNQueens(row + 1, n, queens)) return true;
queens[row] = [1];
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting to 0 may incorrectly indicate a queen placed in column 0.
Setting to row or n is invalid as they are valid indices or out of range.
✗ Incorrect
Resetting queens[row] to -1 indicates no queen placed in that row after backtracking.
3fill in blank
hardFix the error in the greedy approach that tries to solve the subset sum problem.
DSA C#
bool GreedySubsetSum(int[] nums, int target) {
Array.Sort(nums);
int sum = 0;
for (int i = nums.Length - 1; i >= 0; i--) {
if (sum + nums[i] <= target) {
sum += nums[i];
}
}
return sum == [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing sum to 0 always returns false unless target is 0.
Comparing sum to nums.Length is meaningless here.
✗ Incorrect
We check if the sum equals the target to confirm subset sum success.
4fill in blank
hardFill both blanks to create a backtracking function that generates all subsets of a list.
DSA C#
void GenerateSubsets(int[] nums, int index, List<int> current, List<List<int>> result) {
if (index == nums.Length) {
result.Add(new List<int>(current));
return;
}
GenerateSubsets(nums, [1], current, result);
current.Add(nums[index]);
GenerateSubsets(nums, [2], current, result);
current.RemoveAt(current.Count - 1);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index - 1 causes infinite loops or errors.
Using 0 or nums.Length resets or skips incorrectly.
✗ Incorrect
We move to the next index in both recursive calls, once without including and once including the current element.
5fill in blank
hardFill all three blanks to implement a greedy algorithm for activity selection problem.
DSA C#
List<int> SelectActivities(int[] start, int[] finish) {
List<int> selected = new List<int>();
int lastFinish = [1];
for (int i = 0; i < start.Length; i++) {
if (start[i] >= lastFinish) {
selected.Add(i);
lastFinish = [2];
}
}
return selected;
}
// Assume activities are sorted by finish time
// Initialize lastFinish to [3]Attempts:
3 left
💡 Hint
Common Mistakes
Initializing lastFinish to 0 may skip activities starting at 0.
Updating lastFinish incorrectly causes wrong selections.
✗ Incorrect
Initialize lastFinish to -1 to allow first activity selection; update lastFinish to finish[i] after selecting an activity.
