To find all unique triplets in an array that add up to zero.
Click to reveal answer
beginner
Why do we sort the array before solving the Three Sum Problem?
Sorting helps to avoid duplicates easily and allows using two pointers to find pairs efficiently.
Click to reveal answer
intermediate
Explain the two-pointer technique used in the Three Sum Problem.
After fixing one number, use two pointers starting from the next element and the end of the array to find pairs that sum to the negative of the fixed number.
Click to reveal answer
intermediate
How do you avoid duplicate triplets in the Three Sum Problem?
Skip over duplicate numbers for the fixed element and also skip duplicates when moving the two pointers.
Click to reveal answer
intermediate
What is the time complexity of the common Three Sum solution using sorting and two pointers?
O(n^2), where n is the number of elements in the array.
Click to reveal answer
What is the first step in solving the Three Sum Problem efficiently?
ASort the array
BUse a hash map
CReverse the array
DRemove duplicates first
✗ Incorrect
Sorting the array is the first step to help use two pointers and avoid duplicates.
In the two-pointer approach, what do the two pointers represent?
ARandom positions in the array
BStart and end of the array segment after the fixed element
CThe first and last elements of the whole array
DThe smallest and largest elements in the array
✗ Incorrect
The two pointers start just after the fixed element and at the end of the array segment to find pairs.
How do you handle duplicates for the fixed element in the Three Sum Problem?
AUse a hash set to store duplicates
BAlways include duplicates
CRemove duplicates before processing
DSkip the fixed element if it is the same as the previous one
✗ Incorrect
Skipping the fixed element if it is the same as the previous avoids duplicate triplets.
What is the sum target for the two pointers after fixing one element?
AZero
BSum of the fixed element and zero
CNegative of the fixed element
DDouble the fixed element
✗ Incorrect
The two pointers look for pairs that sum to the negative of the fixed element to make total zero.
What is the overall time complexity of the Three Sum solution using sorting and two pointers?
AO(n^2)
BO(n log n)
CO(n^3)
DO(n)
✗ Incorrect
Sorting takes O(n log n), and the two-pointer search inside a loop takes O(n^2) total.
Describe the step-by-step approach to solve the Three Sum Problem with unique triplets.
Think about sorting first, then how to find pairs efficiently.
You got /5 concepts.
Explain how to avoid duplicate triplets in the Three Sum Problem solution.
Duplicates can come from fixed element or from pairs found.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of the Three Sum Problem All Unique Triplets in an array of integers?
easy
A. Find the maximum sum of any three numbers
B. Find all unique triplets that sum to zero
C. Find the three largest numbers in the array
D. Find pairs of numbers that sum to zero
Solution
Step 1: Understand the problem requirement
The problem asks to find all unique sets of three numbers (triplets) in the array whose sum equals zero.
Step 2: Differentiate from other sum problems
Unlike finding pairs or maximum sums, this problem specifically looks for triplets with sum zero and no duplicates.
Final Answer:
Find all unique triplets that sum to zero -> Option B
Quick Check:
Three Sum Problem = Find unique triplets summing to zero [OK]
Hint: Look for three numbers adding exactly to zero [OK]
Common Mistakes:
Confusing with pair sum problems
Looking for maximum sum instead of zero sum
Ignoring uniqueness of triplets
2. Which of the following TypeScript function signatures correctly represents a function solving the Three Sum Problem returning all unique triplets as arrays?
easy
A. function threeSum(nums: number[]): number[][]
B. function threeSum(nums: number): number[]
C. function threeSum(nums: string[]): string[][]
D. function threeSum(nums: number[][]): number[]
Solution
Step 1: Analyze the input parameter type
The input should be an array of numbers, so nums: number[] is correct.
Step 2: Analyze the return type
The function returns an array of triplets, each triplet is an array of numbers, so number[][] is correct.
Final Answer:
function threeSum(nums: number[]): number[][] -> Option A
Quick Check:
Input array of numbers, output array of number triplets [OK]
Hint: Input array of numbers, output array of triplets [OK]
Common Mistakes:
Using wrong input type like single number or string array
Returning flat array instead of array of arrays
Confusing input and output types
3. What is the output of the following TypeScript code snippet?
const nums = [-1, 0, 1, 2, -1, -4];
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const result: number[][] = [];
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 (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(nums));
medium
A. [[-1, 0, 1], [0, 1, 2]]
B. [[-4, 0, 4], [-1, -1, 2]]
C. [[-1, -1, 2], [-1, 0, 1]]
D. [[0, 1, 2], [-1, -1, 2]]
Solution
Step 1: Sort the array
Sorted array is [-4, -1, -1, 0, 1, 2].
Step 2: Find unique triplets summing to zero
Triplets found are [-1, -1, 2] and [-1, 0, 1].
Final Answer:
[[-1, -1, 2], [-1, 0, 1]] -> Option C
Quick Check:
Unique triplets summing zero = [[-1,-1,2],[-1,0,1]] [OK]
Hint: Sort array, use two pointers to find triplets [OK]
Common Mistakes:
Not sorting array before processing
Including duplicate triplets
Incorrect pointer movement causing wrong sums
4. Identify the bug in the following TypeScript code for the Three Sum Problem:
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const result: number[][] = [];
for (let i = 0; i < nums.length - 2; i++) {
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]]);
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result;
}
medium
A. Missing check to skip duplicate 'i' values
B. Incorrect sorting order of the array
C. Wrong initialization of left and right pointers
D. Returning result before loop ends
Solution
Step 1: Check for duplicate skipping
The code does not skip duplicate values for index i, which can cause duplicate triplets.
Step 2: Confirm other parts are correct
Sorting is correct ascending, pointers initialized properly, and return is after loops.
Final Answer:
Missing check to skip duplicate 'i' values -> Option A
Quick Check:
Duplicate skipping for i missing = bug [OK]
Hint: Always skip duplicates for i to avoid repeated triplets [OK]
Common Mistakes:
Not skipping duplicates for i index
Skipping duplicates only for left or right pointers
Incorrect pointer initialization
5. Given the array nums = [-2, 1, 0, -1, 2, -3], which of the following is the correct set of all unique triplets that sum to zero?