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 JavaScript function when called with nums = [1, 0, -1, 0, -2, 2] and target = 0?
DSA Javascript
function fourSum(nums, target) {
nums.sort((a, b) => a - b);
const quadruplets = [];
for (let i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
for (let j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let left = j + 1;
let right = nums.length - 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
Sort the array first and use two pointers to find pairs that sum to the target minus the first two numbers.
✗ Incorrect
The function sorts the array and uses nested loops with two pointers to find all unique quadruplets that sum to the target. It skips duplicates to ensure uniqueness.
🧠 Conceptual
intermediateNumber of Unique Quadruplets for Given Input
Given the array nums = [2, 2, 2, 2, 2] and target = 8, how many unique quadruplets does the four sum algorithm find?
Attempts:
2 left
💡 Hint
All elements are the same, so only one unique quadruplet is possible if sum matches target.
✗ Incorrect
Since all elements are 2, the only quadruplet summing to 8 is [2, 2, 2, 2]. The algorithm avoids duplicates, so it returns one unique quadruplet.
🔧 Debug
advancedIdentify the Error in Four Sum Implementation
What error will occur when running this code snippet for four sum?
DSA Javascript
function fourSum(nums, target) {
nums.sort((a, b) => a - b);
const quadruplets = [];
for (let i = 0; i < nums.length - 3; i++) {
for (let j = i + 1; j < nums.length - 2; j++) {
let left = j + 1;
let right = nums.length - 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;
}Attempts:
2 left
💡 Hint
Check the conditions inside the while loops that skip duplicates.
✗ Incorrect
The inner while loops that skip duplicates do not check if left+1 or right-1 are within array bounds, which can cause left or right pointers to move incorrectly and lead to an infinite loop.
📝 Syntax
advancedCorrect Syntax for Four Sum Filtering Duplicates
Which option correctly skips duplicates in the four sum algorithm inside the nested loops?
Attempts:
2 left
💡 Hint
Duplicates are skipped by continuing the loop when current equals previous element.
✗ Incorrect
Option A correctly skips duplicates by continuing the loop if the current element equals the previous one. Other options either use wrong comparison, wrong indices, or wrong control flow.
🚀 Application
expertMaximum Number of Unique Quadruplets
Given an array of 100 distinct integers sorted in ascending order and target = 0, what is the maximum number of unique quadruplets the four sum algorithm can find?
Attempts:
2 left
💡 Hint
Number of unique quadruplets from n distinct elements is n choose 4.
✗ Incorrect
The maximum number of unique quadruplets from 100 distinct elements is the combination of 100 elements taken 4 at a time, which is 100! / (4! * 96!) = 3,921,225.
