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.[1](), nums.end());Attempts:
3 left
💡 Hint
Common Mistakes
Using rbegin() or rend() which reverse the order and break the logic.
Forgetting to sort the vector before searching quadruplets.
✗ Incorrect
Sorting the vector from begin() to end() is necessary to apply the two-pointer technique correctly.
2fill in blank
mediumComplete the code to skip duplicate elements for the first pointer i.
DSA C++
if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicates for [1]
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping duplicates for the wrong pointer variable.
Not checking i > 0 before accessing nums[i - 1].
✗ Incorrect
We skip duplicates for the first pointer i to avoid repeated quadruplets.
3fill in blank
hardFix the error in the two-pointer while loop condition to avoid infinite loops.
DSA C++
while (left [1] right) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes pointers to overlap and may cause infinite loops.
Using '>' or '>=' reverses the logic and breaks the loop.
✗ Incorrect
The two pointers should move while left is less than right to avoid overlap and infinite loops.
4fill in blank
hardFill both blanks to correctly move the left and right pointers after finding a quadruplet.
DSA C++
while (left < right && nums[left] == nums[left [1]]) left++; while (left < right && nums[right] == nums[right [2]]) right--;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' with left pointer or '+' with right pointer causes wrong movement.
Not skipping duplicates leads to repeated quadruplets.
✗ Incorrect
After finding a quadruplet, move left pointer forward and right pointer backward skipping duplicates.
5fill in blank
hardFill all three blanks to complete the quadruplet insertion and pointer updates.
DSA C++
result.push_back({nums[i], nums[j], nums[left], nums[right]});
left[1];
right[2];
while (left < right && nums[left] == nums[left [3]]) left++;Attempts:
3 left
💡 Hint
Common Mistakes
Using -- for left pointer or ++ for right pointer causes wrong pointer movement.
Not skipping duplicates after pointer updates causes repeated quadruplets.
✗ Incorrect
After adding quadruplet, increment left, decrement right, then skip duplicates by incrementing left.
