Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the input array before processing.
DSA Javascript
nums.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using reverse instead of sort
Trying to filter or map instead of sorting
✗ Incorrect
Sorting the array helps to use two pointers efficiently and avoid duplicates.
2fill in blank
mediumComplete the code to initialize the left pointer for the two-pointer approach.
DSA Javascript
let left = [1] + 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 0
Starting left at right pointer
✗ Incorrect
The left pointer starts just after the current index i to find triplets.
3fill in blank
hardFix the error in the condition to skip duplicate elements for the main loop.
DSA Javascript
if (i > 0 && nums[i] === nums[1]) continue;
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with next element
Comparing with same element
✗ Incorrect
We compare current element with the previous one to skip duplicates.
4fill in blank
hardFill both blanks to correctly move the left and right pointers after finding a valid triplet.
DSA Javascript
while (left < right && nums[left] === nums[left[1]]) left[2]; while (left < right && nums[right] === nums[right[1]]) right[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointer increments
Mixing up left and right pointer movements
✗ Incorrect
We move left pointer forward (+1) and right pointer backward (--), but here we use ++ and -- operators correctly.
5fill in blank
hardFill all three blanks to correctly calculate the sum and update pointers based on the sum.
DSA Javascript
const sum = nums[i] + nums[left] + nums[right]; if (sum === 0) { result.push([nums[i], nums[left], nums[right]]); left[1]; right[2]; } else if (sum [3] 0) { left++; } else { right--; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pointer increments
Wrong comparison operator for sum
✗ Incorrect
When sum is zero, move left forward (++) and right backward (--). If sum is greater than zero, move right pointer left to reduce sum.
