Challenge - 5 Problems
Four Sum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Four Sum Quadruplets for a Given Array and Target
What is the output of the following C++ code that finds all unique quadruplets in the array that sum to the target 0?
DSA C++
std::vector<std::vector<int>> fourSum(std::vector<int>& nums, int target) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n - 3; ++i) { if (i > 0 && nums[i] == nums[i-1]) continue; for (int j = i + 1; j < n - 2; ++j) { if (j > i + 1 && nums[j] == nums[j-1]) continue; int left = j + 1, right = n - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { result.push_back({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 result; } int main() { std::vector<int> nums = {1, 0, -1, 0, -2, 2}; int target = 0; auto res = fourSum(nums, target); for (auto &quad : res) { for (int num : quad) { std::cout << num << " "; } std::cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Think about sorting the array and using two pointers to find pairs that complete the quadruplet.
✗ Incorrect
The code sorts the array and uses two nested loops with two pointers to find quadruplets that sum to the target. It skips duplicates to ensure unique quadruplets. The correct output matches option A.
🧠 Conceptual
intermediateNumber of Unique Quadruplets for Four Sum
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 can sum to 8.
✗ Incorrect
Since all elements are 2, the only quadruplet summing to 8 is [2, 2, 2, 2]. The algorithm skips duplicates, so only one unique quadruplet is found.
🔧 Debug
advancedIdentify the Bug in Four Sum Implementation
What error will the following code produce when run, and why?
DSA C++
std::vector<std::vector<int>> fourSum(std::vector<int>& nums, int target) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n - 3; ++i) { for (int j = i + 1; j < n - 2; ++j) { int left = j + 1, right = n - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { result.push_back({nums[i], nums[j], nums[left], nums[right]}); ++left; --right; } else if (sum < target) { ++left; } else { --right; } } } } return result; }
Attempts:
2 left
💡 Hint
Check if the code handles duplicate quadruplets properly.
✗ Incorrect
The code does not skip duplicate values for i, j, left, or right indices, so it will include duplicate quadruplets but will not cause runtime or compilation errors.
❓ Predict Output
advancedOutput of Four Sum with Negative Target
What is the output of the following code snippet for nums = [-3, -1, 0, 2, 4, 5] and target = 2?
DSA C++
std::vector<std::vector<int>> fourSum(std::vector<int>& nums, int target) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n - 3; ++i) { if (i > 0 && nums[i] == nums[i-1]) continue; for (int j = i + 1; j < n - 2; ++j) { if (j > i + 1 && nums[j] == nums[j-1]) continue; int left = j + 1, right = n - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { result.push_back({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 result; } int main() { std::vector<int> nums = {-3, -1, 0, 2, 4, 5}; int target = 2; auto res = fourSum(nums, target); for (auto &quad : res) { for (int num : quad) { std::cout << num << " "; } std::cout << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Check sums of quadruplets carefully after sorting.
✗ Incorrect
Only one quadruplet sums to 2: [-3, -1, 2, 4]. Other options include numbers not in the array or sums that don't match target.
🚀 Application
expertMinimum Number of Quadruplets to Cover All Elements
Given an array nums = [1, 0, -1, 0, -2, 2], what is the minimum number of unique quadruplets from the Four Sum problem with target 0 needed to cover all elements in the array at least once?
Attempts:
2 left
💡 Hint
Find quadruplets that cover all elements with minimal overlap.
✗ Incorrect
The unique quadruplets are [-2, -1, 1, 2], [-2, 0, 0, 2], and [-1, 0, 0, 1]. To cover all elements (1,0,-1,0,-2,2), at least two quadruplets are needed.
