Challenge - 5 Problems
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Three Sum Unique Triplets Function
What is the output of the following TypeScript function when called with the input [-1, 0, 1, 2, -1, -4]?
DSA Typescript
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;
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]));Attempts:
2 left
💡 Hint
Remember the function sorts the array and skips duplicates to find unique triplets that sum to zero.
✗ Incorrect
The function sorts the input array and uses a two-pointer approach to find all unique triplets that sum to zero. It skips duplicates to avoid repeated triplets. For the input [-1, 0, 1, 2, -1, -4], the unique triplets are [-1, -1, 2] and [-1, 0, 1].
🧠 Conceptual
intermediateUnderstanding the Role of Sorting in Three Sum
Why is sorting the input array important in the Three Sum problem solution?
Attempts:
2 left
💡 Hint
Think about how duplicates and pair searching are handled.
✗ Incorrect
Sorting helps to skip duplicate elements easily by comparing adjacent elements. It also allows the use of the two-pointer technique to find pairs that sum to a target efficiently, reducing the time complexity.
🔧 Debug
advancedIdentify the Error in the Three Sum Implementation
What error will the following code produce when run with input [-2, 0, 1, 1, 2]?
DSA Typescript
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const result: number[][] = [];
for (let i = 0; i < nums.length - 2; i++) {
if (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([-2, 0, 1, 1, 2]));Attempts:
2 left
💡 Hint
Check the condition that skips duplicates carefully.
✗ Incorrect
The condition if (nums[i] === nums[i - 1]) continue; causes a TypeError when i = 0 because nums[-1] is undefined. The correct check should be if (i > 0 && nums[i] === nums[i - 1]) continue; to avoid accessing out-of-bound index.
❓ Predict Output
advancedOutput of Three Sum with No Valid Triplets
What is the output of the threeSum function when called with input [1, 2, -2, -1]?
DSA Typescript
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;
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]));Attempts:
2 left
💡 Hint
Check if any three numbers sum to zero in the sorted array.
✗ Incorrect
No three numbers in the input array sum to zero, so the function returns an empty array.
🚀 Application
expertCount Unique Triplets Summing to Zero
Given the function threeSum that returns all unique triplets summing to zero, what is the count of unique triplets for the input [-3, 0, 1, 2, -1, 1, -2]?
DSA Typescript
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;
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;
}
const triplets = threeSum([-3, 0, 1, 2, -1, 1, -2]);
console.log(triplets.length);Attempts:
2 left
💡 Hint
List all unique triplets manually after sorting the array.
✗ Incorrect
The unique triplets summing to zero are [-3, 1, 2], [-2, 0, 2], and [-1, 0, 1]. There are 3 such triplets.
