Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start backtracking from the first position.
DSA C++
void backtrack(int pos) {
if (pos == n) {
// process solution
return;
}
for (int choice = 0; choice < options; ++choice) {
// make choice
backtrack([1]);
// undo choice
}
}
int main() {
backtrack(0);
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pos - 1 causes infinite recursion backward.
Using 0 resets position and causes repeated work.
Using n skips all positions.
✗ Incorrect
In backtracking, after making a choice at position pos, we move to the next position pos + 1 to continue exploring.
2fill in blank
mediumComplete the code to check if the current partial solution is valid before continuing.
DSA C++
bool isValid(int pos) {
// check constraints for position pos
return [1];
}
void backtrack(int pos) {
if (pos == n) {
// process solution
return;
}
if (!isValid(pos)) return;
for (int choice = 0; choice < options; ++choice) {
// make choice
backtrack(pos + 1);
// undo choice
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning false always stops the search.
Checking pos == n here is incorrect; that is the base case.
✗ Incorrect
The isValid function should return true if the partial solution is valid so far, allowing backtracking to continue.
3fill in blank
hardFix the error in the backtracking recursive call to avoid infinite recursion.
DSA C++
void backtrack(int pos) {
if (pos == n) {
// process solution
return;
}
for (int choice = 0; choice < options; ++choice) {
// make choice
backtrack([1]);
// undo choice
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling backtrack(pos) repeats the same call endlessly.
Calling backtrack(pos - 1) goes backward and may cause errors.
✗ Incorrect
The recursive call must move forward by calling backtrack(pos + 1) to avoid infinite recursion at the same position.
4fill in blank
hardFill both blanks to generate all subsets of a set using backtracking.
DSA C++
void backtrack(int pos, vector<int>& subset) {
if (pos == n) {
result.push_back(subset);
return;
}
// Exclude current element
backtrack([1], subset);
// Include current element
subset.push_back(nums[pos]);
backtrack([2], subset);
subset.pop_back();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pos instead of pos + 1 causes infinite recursion.
Using pos - 1 goes backward and causes errors.
✗ Incorrect
In both recursive calls, we move forward by increasing pos by 1 to explore subsets including or excluding the current element.
5fill in blank
hardFill all three blanks to build a decision tree for permutations using backtracking.
DSA C++
void backtrack(vector<int>& current, vector<bool>& used) {
if (current.size() == n) {
result.push_back(current);
return;
}
for (int i = 0; i < n; ++i) {
if (!used[i]) {
used[i] = true;
current.push_back(nums[[1]]);
backtrack(current, used);
current.pop_back();
used[[2]] = false;
}
}
}
int main() {
vector<int> current;
vector<bool> used(n, false);
backtrack(current, used);
return 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pos instead of i causes undefined variable errors.
Using n or 0 instead of i breaks the logic.
✗ Incorrect
The index i is used to track which element is chosen and marked used in the permutation backtracking.
