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#
using System; using System.Collections.Generic; class Program { static IList<IList<int>> ThreeSum(int[] nums) { Array.Sort(nums); var result = new List<IList<int>>(); for (int i = 0; i < nums.Length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.Length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.Add(new List<int>{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; } static void Main() { int[] nums = new int[] {-1, 0, 1, 2, -1, -4}; var triplets = ThreeSum(nums); foreach (var triplet in triplets) { Console.WriteLine(string.Join(", ", triplet)); } } }
Attempts:
2 left
💡 Hint
Think about sorting the array and skipping duplicates to find unique triplets.
✗ Incorrect
The code sorts the array and uses a two-pointer approach to find triplets that sum to zero. It skips duplicates to ensure unique triplets. The output lists the two unique triplets: [-1, -1, 2] and [-1, 0, 1].
🧠 Conceptual
intermediateUnderstanding the Two-Pointer Technique in Three Sum
Why does the two-pointer technique work efficiently after sorting the array in the Three Sum problem?
Attempts:
2 left
💡 Hint
Think about how sorting helps in skipping unnecessary checks.
✗ Incorrect
Sorting the array lets us use two pointers moving inward to find pairs that sum to a target efficiently, reducing the complexity from cubic to quadratic time.
🔧 Debug
advancedIdentify the Bug in Triplet Sum Code
What error will the following code produce when run, and why?
int[] nums = {0, 0, 0};
var result = ThreeSum(nums);
Console.WriteLine(result.Count);
DSA C#
static IList<IList<int>> ThreeSum(int[] nums) { Array.Sort(nums); var result = new List<IList<int>>(); for (int i = 0; i < nums.Length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.Length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.Add(new List<int>{nums[i], nums[left], nums[right]}); left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return result; }
Attempts:
2 left
💡 Hint
Check if duplicates are skipped after finding a valid triplet.
✗ Incorrect
The code finds one triplet [0,0,0] summing to zero. Although it does not skip duplicates after adding a triplet, it still returns the correct count 1 without error.
🚀 Application
advancedNumber of Unique Triplets Summing to Zero
Given the array [-2, 0, 1, 1, 2], how many unique triplets sum to zero?
Attempts:
2 left
💡 Hint
Try to find all triplets manually after sorting.
✗ Incorrect
The unique triplets are [-2, 0, 2] and [-2, 1, 1]. So total 2 unique triplets.
❓ Predict Output
expertOutput of Three Sum with No Valid Triplets
What is the output of the following code when the input array has no triplets summing to zero?
DSA C#
using System; using System.Collections.Generic; class Program { static IList<IList<int>> ThreeSum(int[] nums) { Array.Sort(nums); var result = new List<IList<int>>(); for (int i = 0; i < nums.Length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.Length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { result.Add(new List<int>{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; } static void Main() { int[] nums = new int[] {1, 2, 3, 4, 5}; var triplets = ThreeSum(nums); Console.WriteLine(triplets.Count); } }
Attempts:
2 left
💡 Hint
If no triplets sum to zero, the result list remains empty.
✗ Incorrect
Since no three numbers sum to zero, the result list is empty and its count is zero.
