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 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]));Attempts:
2 left
💡 Hint
Sort the array first and use two pointers to find pairs that sum with nums[i] to zero.
✗ Incorrect
The function sorts the array and uses a fixed pointer with two moving pointers to find unique triplets that sum to zero. It skips duplicates to ensure uniqueness.
🧠 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 two pointers work after sorting.
✗ Incorrect
Sorting allows skipping duplicates by comparing adjacent elements and enables the two-pointer technique to find pairs summing to a target efficiently.
🔧 Debug
advancedIdentify 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]));
Attempts:
2 left
💡 Hint
Check how duplicates are handled in the code.
✗ Incorrect
The code does not skip duplicate elements for i, left, or right pointers, so duplicate triplets appear in the result.
❓ Predict Output
advancedOutput 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]));Attempts:
2 left
💡 Hint
Check if any three numbers sum to zero.
✗ Incorrect
No three numbers in the array sum to zero, so the result is an empty array.
🧠 Conceptual
expertTime 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?
Attempts:
2 left
💡 Hint
Consider sorting and nested loops with two pointers.
✗ Incorrect
Sorting takes O(n log n). The main loop runs O(n) times, and inside it, two pointers scan the array in O(n) total, resulting in O(n^2) overall.
