Challenge - 5 Problems
Four Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Four Sum Unique Quadruplets Function
What is the output of the following TypeScript function when called with nums = [1, 0, -1, 0, -2, 2] and target = 0?
DSA Typescript
function fourSum(nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b);
const quadruplets: number[][] = [];
const n = nums.length;
for (let i = 0; i < n - 3; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
for (let j = i + 1; j < n - 2; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let left = j + 1;
let right = n - 1;
while (left < right) {
const sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum === target) {
quadruplets.push([nums[i], nums[j], 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 < target) {
left++;
} else {
right--;
}
}
}
}
return quadruplets;
}
console.log(fourSum([1, 0, -1, 0, -2, 2], 0));Attempts:
2 left
💡 Hint
Think about sorting and skipping duplicates to get unique quadruplets.
✗ Incorrect
The function sorts the array and uses two nested loops with two pointers to find quadruplets that sum to the target. It skips duplicates to ensure uniqueness. The correct quadruplets are [-2,-1,1,2], [-2,0,0,2], and [-1,0,0,1].
🧠 Conceptual
intermediateUnderstanding the Role of Sorting in Four Sum
Why is sorting the input array important in the Four Sum problem when finding all unique quadruplets?
Attempts:
2 left
💡 Hint
Think about how duplicates are handled and how two pointers move.
✗ Incorrect
Sorting the array allows the algorithm to skip duplicates easily by comparing adjacent elements and enables the two-pointer technique to find pairs efficiently.
🔧 Debug
advancedIdentify the Error in Four Sum Implementation
What error will occur when running this code snippet for the Four Sum problem?
DSA Typescript
function fourSum(nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b);
const quadruplets: number[][] = [];
const n = nums.length;
for (let i = 0; i < n - 3; i++) {
for (let j = i + 1; j < n - 2; j++) {
let left = j + 1;
let right = n - 1;
while (left < right) {
const sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum === target) {
quadruplets.push([nums[i], nums[j], nums[left], nums[right]]);
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
}
return quadruplets;
}
console.log(fourSum([1, 0, -1, 0, -2, 2], 0));Attempts:
2 left
💡 Hint
Check how duplicates are handled in the loops.
✗ Incorrect
The code does not skip duplicate values for i, j, left, or right, so it will include repeated quadruplets in the result.
❓ Predict Output
advancedOutput of Four Sum with Negative Target
What is the output of the fourSum function when called with nums = [2, 2, 2, 2, 2] and target = 8?
DSA Typescript
function fourSum(nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b);
const quadruplets: number[][] = [];
const n = nums.length;
for (let i = 0; i < n - 3; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
for (let j = i + 1; j < n - 2; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let left = j + 1;
let right = n - 1;
while (left < right) {
const sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum === target) {
quadruplets.push([nums[i], nums[j], 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 < target) {
left++;
} else {
right--;
}
}
}
}
return quadruplets;
}
console.log(fourSum([2, 2, 2, 2, 2], 8));Attempts:
2 left
💡 Hint
All elements are the same and sum to 8 when four are added.
✗ Incorrect
The only quadruplet is [2,2,2,2] which sums to 8. Duplicates are skipped so only one quadruplet is returned.
🧠 Conceptual
expertTime Complexity of Four Sum Algorithm
What is the time complexity of the standard Four Sum algorithm that uses sorting and two nested loops with two pointers?
Attempts:
2 left
💡 Hint
Consider the loops and the two-pointer approach inside.
✗ Incorrect
The algorithm sorts the array (O(n log n)) and then uses two nested loops (O(n^2)) and a two-pointer scan (O(n)) inside, resulting in O(n^3) overall.
