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 C++ code that finds all unique triplets in the array which gives the sum of zero?
DSA C++
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); for (int i = 0; i < (int)nums.size() - 2; ++i) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = (int)nums.size() - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.push_back({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; } int main() { std::vector<int> nums = {-1, 0, 1, 2, -1, -4}; auto triplets = threeSum(nums); for (auto &triplet : triplets) { for (int num : triplet) { std::cout << num << " "; } std::cout << "-> "; } std::cout << "null" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember the function sorts the array and skips duplicates to find unique triplets summing to zero.
✗ Incorrect
The function sorts the input array and uses a two-pointer approach to find triplets that sum to zero. It skips duplicates to ensure unique triplets. The output prints the triplets in sorted order as found: [-1, -1, 2] and [-1, 0, 1].
🧠 Conceptual
intermediateNumber of Unique Triplets in Three Sum
Given the input array nums = [3, -2, 1, 0, -1, 2, -1], how many unique triplets sum to zero?
Attempts:
2 left
💡 Hint
Try to find all sets of three numbers that add up to zero without repeating the same triplet.
✗ Incorrect
The unique triplets are: [-2, -1, 3], [-2, 0, 2], [-1, -1, 2], [-1, 0, 1]. So total unique triplets are 4.
🔧 Debug
advancedIdentify the Error in Three Sum Implementation
What error will the following code produce when run?
DSA C++
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); for (int i = 0; i < (int)nums.size() - 2; ++i) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = (int)nums.size() - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.push_back({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; } int main() { std::vector<int> nums = {0, 0, 0}; auto triplets = threeSum(nums); for (auto &triplet : triplets) { for (int num : triplet) { std::cout << num << " "; } std::cout << "-> "; } std::cout << "null" << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check how the code handles duplicates and the case when all elements are zero.
✗ Incorrect
The code correctly finds the triplet [0, 0, 0] that sums to zero and outputs it. No errors or infinite loops occur.
📝 Syntax
advancedIdentify the Syntax Error in Three Sum Code
Which option contains a syntax error in the three sum function?
DSA C++
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) { std::vector<std::vector<int>> result; std::sort(nums.begin(), nums.end()); for (int i = 0; i < (int)nums.size() - 2; ++i) { if (i > 0 && nums[i] == nums[i-1]) continue; int left = i + 1, right = (int)nums.size() - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.push_back({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; }
Attempts:
2 left
💡 Hint
Check the syntax of push_back with initializer list in C++.
✗ Incorrect
Using curly braces {} inside push_back is valid in C++11 and later for initializer lists. However, if the compiler does not support it or if the code is older style, it may cause syntax error. The other options are incorrect because semicolons are present, comparison operator is correct, and return statement exists.
🚀 Application
expertOptimizing Three Sum for Large Input
You have a very large array with millions of integers. Which approach is the most efficient to find all unique triplets that sum to zero?
Attempts:
2 left
💡 Hint
Consider time complexity and how sorting helps reduce it.
✗ Incorrect
Sorting the array and using two pointers for each element reduces the time complexity to O(n^2), which is efficient for large inputs compared to brute force O(n^3). Hash map approach can be less efficient due to extra space and complexity. Recursion for all triplets is very inefficient.
