Challenge - 5 Problems
Four Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Four Sum Unique Quadruplets Function
What is the output of the following code that finds all unique quadruplets in the array that sum to the target 0?
DSA C
void fourSum(int* nums, int numsSize, int target) { // Assume nums is sorted for (int i = 0; i < numsSize - 3; i++) { if (i > 0 && nums[i] == nums[i-1]) continue; for (int j = i + 1; j < numsSize - 2; j++) { if (j > i + 1 && nums[j] == nums[j-1]) continue; int left = j + 1, right = numsSize - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { printf("[%d, %d, %d, %d]\n", 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--; } } } } } int main() { int nums[] = {1, 0, -1, 0, -2, 2}; int size = sizeof(nums)/sizeof(nums[0]); // Sort the array for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (nums[j] > nums[j+1]) { int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } fourSum(nums, size, 0); return 0; }
Attempts:
2 left
💡 Hint
Remember to sort the array first and skip duplicates to get unique quadruplets.
✗ Incorrect
The code sorts the array and uses two nested loops with two pointers to find quadruplets summing to zero. It skips duplicates to ensure unique quadruplets. The correct quadruplets are [-2, -1, 1, 2], [-2, 0, 0, 2], and [-1, 0, 0, 1].
🧠 Conceptual
intermediate1:00remaining
Understanding the Role of Sorting in Four Sum
Why is sorting the array important before finding all unique quadruplets that sum to a target?
Attempts:
2 left
💡 Hint
Think about how duplicates and two pointers work.
✗ Incorrect
Sorting allows the algorithm to skip duplicates easily by comparing adjacent elements and enables the two-pointer technique to find pairs efficiently.
🔧 Debug
advanced1:30remaining
Identify the Bug in Quadruplet Finding Code
What error will occur if the inner while loops that skip duplicates are removed from the four sum code?
DSA C
while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { printf("[%d, %d, %d, %d]\n", nums[i], nums[j], nums[left], nums[right]); // Duplicate skipping loops removed here left++; right--; } else if (sum < target) { left++; } else { right--; } }
Attempts:
2 left
💡 Hint
Think about what skipping duplicates prevents.
✗ Incorrect
Without skipping duplicates, the same quadruplet can be printed multiple times because the pointers move without checking for repeated values.
❓ Predict Output
advanced2:00remaining
Output with Negative Target Sum
What is the output of the four sum function when the input array is [-3, -1, 0, 2, 4, 5] and the target is 2?
DSA C
int nums[] = {-3, -1, 0, 2, 4, 5}; int size = sizeof(nums)/sizeof(nums[0]); // Sort the array for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - i - 1; j++) { if (nums[j] > nums[j+1]) { int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } fourSum(nums, size, 2);
Attempts:
2 left
💡 Hint
Check sums carefully and remember the array is sorted.
✗ Incorrect
Only the quadruplet [-3, -1, 2, 4] sums to 2. Other options include numbers not in the array or sums that don't match.
🧠 Conceptual
expert1:00remaining
Time Complexity of Four Sum Algorithm
What is the time complexity of the standard four sum algorithm that uses sorting and two pointers to find all unique quadruplets?
Attempts:
2 left
💡 Hint
Consider the nested loops and two-pointer approach.
✗ Incorrect
The algorithm uses two nested loops (O(n^2)) and inside uses two pointers moving linearly (O(n)), resulting in O(n^3) time complexity.
