Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking recursion from the first element.
DSA C++
void backtrack(int index) {
if (index == n) {
// process solution
return;
}
// try options
}
int main() {
int n = 5;
backtrack([1]);
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from index 1 skips the first element.
Starting from n means no elements to process.
Starting from -1 is invalid index.
✗ Incorrect
Backtracking usually starts from index 0 to explore all options from the beginning.
2fill in blank
mediumComplete the code to check if the current partial solution is valid before continuing.
DSA C++
bool isValid(vector<int>& solution) {
// check constraints
return solution.size() <= [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 always returns false except empty solution.
Using solution.size() compares size to itself, always true.
Using INT_MAX is too large and not meaningful here.
✗ Incorrect
We check if the solution size does not exceed the problem size n.
3fill in blank
hardFix the error in the backtracking recursive call to explore next options.
DSA C++
void backtrack(int index) {
if (index == n) {
// process solution
return;
}
for (int choice = 0; choice < options.size(); ++choice) {
solution.push_back(options[choice]);
backtrack([1]);
solution.pop_back();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using index again causes infinite recursion.
Using choice as index is incorrect because choice is option index.
Using 0 restarts recursion endlessly.
✗ Incorrect
We must move to the next index to avoid infinite recursion and explore next positions.
4fill in blank
hardFill both blanks to create a condition where greedy fails but backtracking succeeds.
DSA C++
vector<int> nums = {2, 4, 6, 10};
int target = 16;
bool canSum(int sum, int index) {
if (sum == target) return true;
if (sum > target || index == [1]) return false;
return canSum(sum + nums[index], index + 1) || canSum(sum, [2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as index stop condition causes infinite recursion.
Using target as index is invalid.
Not increasing index in recursive calls causes infinite loops.
✗ Incorrect
We stop when index reaches nums.size(), and explore skipping current index by calling canSum with index + 1.
5fill in blank
hardFill all three blanks to implement a backtracking solution that finds all subsets summing to target, showing why greedy fails.
DSA C++
void findSubsets(int index, int currentSum) {
if (currentSum == target) {
results.push_back(currentSubset);
return;
}
if (index == [1] || currentSum > target) return;
// include nums[index]
currentSubset.push_back(nums[[2]]);
findSubsets(index + 1, currentSum + nums[[3]]);
currentSubset.pop_back();
// exclude nums[index]
findSubsets(index + 1, currentSum);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using target as index limit causes errors.
Using wrong index to access nums causes out-of-range errors.
Not increasing index causes infinite recursion.
✗ Incorrect
Stop when index reaches nums.size(), include nums[index] by accessing nums[index], and recurse with index + 1 to explore all subsets.
