Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the input vector before processing.
DSA C++
std::sort(nums.begin(), nums.[1]());Attempts:
3 left
💡 Hint
Common Mistakes
Using rbegin() or rend() which are reverse iterators and cause errors.
Forgetting to sort the vector before processing.
✗ Incorrect
We need to sort the vector from begin() to end() to process elements in order.
2fill in blank
mediumComplete the code to skip duplicate elements for the first pointer i.
DSA C++
if (i > 0 && nums[i] == nums[[1]]) continue;
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with nums[i+1] which may go out of bounds.
Not checking i > 0 before accessing nums[i-1].
✗ Incorrect
We compare current element nums[i] with the previous nums[i-1] to skip duplicates.
3fill in blank
hardFix the error in the while loop condition to move the left pointer correctly.
DSA C++
while (left < right && nums[left] == nums[[1]]) left++;
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with nums[left + 1] which is not yet moved.
Comparing with nums[right] which is unrelated here.
✗ Incorrect
We compare nums[left] with nums[left - 1] to skip duplicates after incrementing left.
4fill in blank
hardFill both blanks to correctly move the right pointer and skip duplicates.
DSA C++
while (left < right && nums[right] == nums[[1]]) [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing right pointer which goes out of bounds.
Comparing with left pointer values.
✗ Incorrect
We compare nums[right] with nums[right - 1] and decrement right to skip duplicates.
5fill in blank
hardFill all three blanks to correctly update pointers based on sum comparison.
DSA C++
if (sum < 0) [1]; else if (sum > 0) [2]; else { result.push_back({nums[i], nums[left], nums[right]}); [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving wrong pointers for sum comparison.
Not moving both pointers after finding a valid triplet.
✗ Incorrect
If sum is less than zero, move left pointer right to increase sum; if greater, move right pointer left to decrease sum; if equal, add triplet and move both pointers.
