Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Three Sum Problem All Unique Triplets in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Three Sum Unique Triplets Function
What is the output of the following JavaScript function when called with nums = [-1, 0, 1, 2, -1, -4]?
DSA Javascript
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let left = i + 1;
    let right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (left < right && nums[left] === nums[left + 1]) left++;
        while (left < right && nums[right] === nums[right - 1]) right--;
        left++;
        right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}

console.log(threeSum([-1, 0, 1, 2, -1, -4]));
A[[-1, -1, 2], [-1, 0, 1]]
B[[-1, 0, 1], [0, 1, -1]]
C[[-4, 2, 2], [-1, 0, 1]]
D[[0, 0, 0], [-1, -1, 2]]
Attempts:
2 left
💡 Hint
Sort the array first and use two pointers to find pairs that sum with nums[i] to zero.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of Sorting in Three Sum
Why is sorting the input array important in the Three Sum problem solution?
AIt helps to skip duplicate elements easily and use two pointers efficiently.
BIt makes the array elements all positive, simplifying the sum calculation.
CIt reduces the array size by removing duplicates before processing.
DIt allows the use of binary search to find the third element in O(1) time.
Attempts:
2 left
💡 Hint
Think about how duplicates and two pointers work after sorting.
🔧 Debug
advanced
2:00remaining
Identify the Error in Three Sum Implementation
What error will the following code produce when run with nums = [-2,0,0,2,2]? function threeSum(nums) { nums.sort((a, b) => a - b); const result = []; for (let i = 0; i < nums.length - 2; i++) { let left = i + 1; let right = nums.length - 1; while (left < right) { const sum = nums[i] + nums[left] + nums[right]; if (sum === 0) { result.push([nums[i], nums[left], nums[right]]); left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return result; } console.log(threeSum([-2,0,0,2,2]));
AThe function returns an empty array because the loop condition is wrong.
BSyntaxError due to missing semicolon after result.push line.
CTypeError because nums is not defined.
DThe output contains duplicate triplets because duplicates are not skipped.
Attempts:
2 left
💡 Hint
Check how duplicates are handled in the code.
❓ Predict Output
advanced
1:30remaining
Output of Three Sum with No Triplets
What is the output of the threeSum function when called with nums = [1, 2, -2, -1]?
DSA Javascript
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let left = i + 1;
    let right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (left < right && nums[left] === nums[left + 1]) left++;
        while (left < right && nums[right] === nums[right - 1]) right--;
        left++;
        right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}

console.log(threeSum([1, 2, -2, -1]));
A[[1, -1, 0]]
B[]
C[[-2, -1, 3]]
D[[1, 2, -3]]
Attempts:
2 left
💡 Hint
Check if any three numbers sum to zero.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Three Sum Algorithm
What is the time complexity of the standard Three Sum algorithm that sorts the array and uses two pointers to find triplets?
AO(n^3) because it checks all triplets.
BO(n log n) because sorting dominates the complexity.
CO(n^2) because sorting is O(n log n) and two pointers run in O(n^2).
DO(n) because two pointers scan the array once.
Attempts:
2 left
💡 Hint
Consider sorting and nested loops with two pointers.

Practice

(1/5)
1. What is the main goal of the Three Sum Problem in algorithms?
easy
A. Find all unique triplets in an array that sum to zero
B. Sort the array in ascending order
C. Find the maximum sum of any three elements
D. Count the number of pairs that sum to zero

Solution

  1. Step 1: Understand the problem statement

    The Three Sum Problem asks to find all unique sets of three numbers in an array whose sum is zero.
  2. Step 2: Differentiate from other sum problems

    Unlike sorting or pair sums, this problem specifically targets triplets summing to zero without duplicates.
  3. Final Answer:

    Find all unique triplets in an array that sum to zero -> Option A
  4. Quick Check:

    Three Sum Problem = Find unique triplets summing to zero [OK]
Hint: Look for three numbers adding to zero without repeats [OK]
Common Mistakes:
  • Confusing with pair sum problems
  • Thinking it requires sorting only
  • Looking for max sum instead of zero sum
2. Which of the following is the correct way to start sorting an array nums in JavaScript before solving the Three Sum Problem?
easy
A. nums.sort()
B. nums.sorted()
C. nums.sort((a, b) => b - a)
D. nums.sort((a, b) => a - b)

Solution

  1. Step 1: Understand JavaScript sort behavior

    By default, sort() converts elements to strings and sorts lexicographically, which is incorrect for numbers.
  2. Step 2: Use numeric comparator for ascending order

    Using nums.sort((a, b) => a - b) sorts numbers correctly in ascending order, which is needed for the algorithm.
  3. Final Answer:

    nums.sort((a, b) => a - b) -> Option D
  4. Quick Check:

    Numeric ascending sort = nums.sort((a, b) => a - b) [OK]
Hint: Use comparator (a, b) => a - b for numeric ascending sort [OK]
Common Mistakes:
  • Using default sort() without comparator
  • Using descending sort when ascending needed
  • Using non-existent method sorted()
3. What will be the output of the following code snippet?
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (i > 0 && nums[i] === nums[i - 1]) continue;
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (nums[left] === nums[left + 1]) left++;
        while (nums[right] === nums[right - 1]) right--;
        left++;
        right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}

console.log(threeSum([-1,0,1,2,-1,-4]));
medium
A. [[-1, 0, 1], [0, 1, 2]]
B. [[-1, -1, 2], [-1, 0, 1]]
C. [[-4, 2, 2], [-1, 0, 1]]
D. [[0, 0, 0]]

Solution

  1. Step 1: Sort the array

    Sorted array is [-4, -1, -1, 0, 1, 2].
  2. Step 2: Find unique triplets summing to zero

    Triplets found are [-1, -1, 2] and [-1, 0, 1]. These are pushed to result.
  3. Final Answer:

    [[-1, -1, 2], [-1, 0, 1]] -> Option B
  4. Quick Check:

    Unique triplets summing zero = [[-1,-1,2],[-1,0,1]] [OK]
Hint: Sort array, use two pointers to find zero sum triplets [OK]
Common Mistakes:
  • Not sorting before two-pointer approach
  • Including duplicate triplets
  • Incorrect pointer movement logic
4. Identify the bug in this Three Sum implementation snippet:
function threeSum(nums) {
  nums.sort((a, b) => a - b);
  const result = [];
  for (let i = 0; i < nums.length - 2; i++) {
    if (nums[i] === nums[i - 1]) continue;
    let left = i + 1, right = nums.length - 1;
    while (left < right) {
      const sum = nums[i] + nums[left] + nums[right];
      if (sum === 0) {
        result.push([nums[i], nums[left], nums[right]]);
        while (nums[left] === nums[left + 1]) left++;
        while (nums[right] === nums[right - 1]) right--;
        left++;
        right--;
      } else if (sum < 0) {
        left++;
      } else {
        right--;
      }
    }
  }
  return result;
}
medium
A. The duplicate check for nums[i] should skip when i > 0
B. The sorting comparator is incorrect
C. The pointers left and right are not updated
D. The result array is not initialized

Solution

  1. Step 1: Check duplicate skipping condition

    The condition if (nums[i] === nums[i - 1]) continue; runs even when i = 0, causing incorrect skipping.
  2. Step 2: Correct condition to skip duplicates only if i > 0

    It should be if (i > 0 && nums[i] === nums[i - 1]) continue; to avoid skipping first element.
  3. Final Answer:

    The duplicate check for nums[i] should skip when i > 0 -> Option A
  4. Quick Check:

    Duplicate skip needs i > 0 check [OK]
Hint: Check index bounds when skipping duplicates [OK]
Common Mistakes:
  • Skipping duplicates without index check
  • Wrong sorting comparator
  • Forgetting to update pointers
5. Given the array nums = [3, 0, -2, -1, 1, 2], which of the following is the correct set of unique triplets that sum to zero?
hard
A. [[-2, 2, 3], [-1, 0, 1]]
B. [[-2, -1, 3], [-1, 1, 2]]
C. [[-2, -1, 3], [-2, 0, 2], [-1, 0, 1]]
D. [[0, 0, 0], [-1, 1, 0]]

Solution

  1. Step 1: Sort the array

    Sorted array is [-2, -1, 0, 1, 2, 3].
  2. Step 2: Find all unique triplets summing to zero

    Triplets are [-2, -1, 3], [-2, 0, 2], and [-1, 0, 1].
  3. Final Answer:

    [[-2, -1, 3], [-2, 0, 2], [-1, 0, 1]] -> Option C
  4. Quick Check:

    Sorted + two pointers find unique zero sum triplets [OK]
Hint: Sort and use two pointers to find all zero sum triplets [OK]
Common Mistakes:
  • Missing some triplets
  • Including duplicates
  • Incorrect triplet sums
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard