Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking function call with initial parameters.
DSA C++
backtrack(nums, [1], target, {}, result);Attempts:
3 left
💡 Hint
Common Mistakes
Starting from index 1 skips the first element.
Using negative index causes out-of-bound errors.
✗ Incorrect
The backtracking starts from index 0 to consider all elements from the beginning.
2fill in blank
mediumComplete the condition to stop recursion when the target sum is reached.
DSA C++
if (target [1] 0) { result.push_back(path); return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' causes incorrect stopping conditions.
Using '!=' adds incomplete or invalid combinations.
✗ Incorrect
When target equals zero, the current path sums exactly to the target, so we add it to results.
3fill in blank
hardFix the error in the loop to avoid going out of bounds.
DSA C++
for (int i = start; i [1] nums.size(); ++i) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bound access.
Using '>' or '>=' causes the loop to never run or run incorrectly.
✗ Incorrect
The loop must run while i is less than nums.size() to avoid accessing invalid indices.
4fill in blank
hardFill both blanks to correctly update the path and target in recursion.
DSA C++
path.push_back(nums[i]); backtrack(nums, i + 1, target [1] nums[i], path, result); path.[2]();
Attempts:
3 left
💡 Hint
Common Mistakes
Adding nums[i] to target instead of subtracting.
Using push_back instead of pop_back to remove element.
✗ Incorrect
We subtract nums[i] from target and remove last element from path after recursion to backtrack.
5fill in blank
hardFill all three blanks to correctly skip duplicates and avoid repeated combinations.
DSA C++
if (i > start && nums[i] [1] nums[i [2] 1]) continue; backtrack(nums, i [3], target - nums[i], path, result);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes skipping wrong elements.
Using i-1 instead of i+1 in recursion causes infinite loops.
✗ Incorrect
We skip if current equals previous to avoid duplicates, and move forward with i+1 in recursion.
