Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a subset sum equals target using backtracking.
DSA Java
boolean subsetSum(int[] nums, int target, int index) {
if (target == 0) return true;
if (index == nums.length || target < 0) return false;
// Choose to include nums[index]
if (subsetSum(nums, target - nums[index], index + 1)) return true;
// Choose to exclude nums[index]
return subsetSum(nums, target, [1]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index - 1 causes infinite recursion or wrong checks.
Using target + 1 is incorrect because target is not an index.
✗ Incorrect
We move to the next index when excluding the current number, so index + 1 is correct.
2fill in blank
mediumComplete the code to find the maximum sum of non-adjacent elements using greedy approach.
DSA Java
int maxSumNonAdjacent(int[] nums) {
int incl = 0, excl = 0;
for (int num : nums) {
int new_excl = Math.max(incl, excl);
incl = excl + num;
excl = [1];
}
return Math.max(incl, excl);
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning excl to incl causes wrong sums.
Assigning excl to num is incorrect as num is current element.
✗ Incorrect
We update excl to new_excl which holds the max of previous incl and excl.
3fill in blank
hardFix the error in the backtracking code to generate all subsets.
DSA Java
void generateSubsets(int[] nums, int index, List<Integer> current, List<List<Integer>> result) {
if (index == nums.length) {
result.add(new ArrayList<>(current));
return;
}
// Include nums[index]
current.add(nums[index]);
generateSubsets(nums, index + 1, current, result);
// Exclude nums[index]
[1];
generateSubsets(nums, index + 1, current, result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Not removing the element causes wrong subsets with duplicates.
Clearing result or changing index incorrectly breaks logic.
✗ Incorrect
We must remove the last added element before exploring exclusion path to avoid duplicates.
4fill in blank
hardFill both blanks to create a backtracking solution that finds all permutations of a string.
DSA Java
void permute(String str, String chosen, List<String> result) {
if (str.length() == 0) {
result.add(chosen);
} else {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
String rest = str.substring(0, [1]) + str.substring([2]);
permute(rest, chosen + c, result);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using i-1 causes errors as negative index is invalid.
Using 0 for both blanks does not remove the character.
✗ Incorrect
We remove the character at index i by taking substring before i and after i+1.
5fill in blank
hardFill all three blanks to implement a greedy algorithm to select intervals with maximum non-overlapping intervals.
DSA Java
List<Interval> selectIntervals(List<Interval> intervals) {
Collections.sort(intervals, (a, b) -> a.[1] - b.[1]);
List<Interval> result = new ArrayList<>();
int end = Integer.MIN_VALUE;
for (Interval interval : intervals) {
if (interval.[2] >= end) {
result.add(interval);
end = interval.[3];
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start or length changes the greedy strategy.
Using wrong fields for comparison causes incorrect selections.
✗ Incorrect
We sort intervals by finish time, check if current start >= last end, then update end to current finish.
