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 Unique Quadruplets
What is the output of the following C# code that finds all unique quadruplets summing to target 0?
DSA C#
using System; using System.Collections.Generic; using System.Linq; class Program { static IList<IList<int>> FourSum(int[] nums, int target) { Array.Sort(nums); var res = new List<IList<int>>(); int n = nums.Length; 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) { res.Add(new List<int>{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 res; } static void Main() { int[] nums = {1, 0, -1, 0, -2, 2}; int target = 0; var result = FourSum(nums, target); foreach (var quad in result) { Console.WriteLine(string.Join(", ", quad)); } } }
Attempts:
2 left
💡 Hint
Remember the code sorts the array and skips duplicates to avoid repeated quadruplets.
✗ Incorrect
The code sorts the array and uses two nested loops with two pointers to find quadruplets summing to target. It skips duplicates to ensure unique quadruplets. The output lists all unique quadruplets summing to zero.
🧠 Conceptual
intermediateUnderstanding Duplicate Skipping in Four Sum
Why does the Four Sum solution skip elements when nums[i] == nums[i - 1] or nums[j] == nums[j - 1]?
Attempts:
2 left
💡 Hint
Think about how duplicates affect the uniqueness of quadruplets.
✗ Incorrect
Skipping duplicates at the i and j loops prevents generating the same quadruplet multiple times, ensuring all quadruplets in the result are unique.
🔧 Debug
advancedIdentify the Error in Modified Four Sum Code
What error will this modified Four Sum code produce when run?
DSA C#
static IList<IList<int>> FourSum(int[] nums, int target) { Array.Sort(nums); var res = new List<IList<int>>(); int n = nums.Length; 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) { res.Add(new List<int>{nums[i], nums[j], nums[left], nums[right]}); left++; right--; } else if (sum < target) { left++; } else { right--; } } } } return res; }
Attempts:
2 left
💡 Hint
Check if duplicates are handled and if pointers move correctly.
✗ Incorrect
The code runs without exceptions but does not skip duplicates, so the result may contain repeated quadruplets.
❓ Predict Output
advancedOutput of Four Sum with Negative Target
What is the output of this Four Sum code with target = -1?
DSA C#
int[] nums = {2, 1, 0, -1, -2, -1}; int target = -1; var result = FourSum(nums, target); foreach (var quad in result) { Console.WriteLine(string.Join(", ", quad)); } static IList<IList<int>> FourSum(int[] nums, int target) { Array.Sort(nums); var res = new List<IList<int>>(); int n = nums.Length; 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) { res.Add(new List<int>{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 res; }
Attempts:
2 left
💡 Hint
Check the sorted array and carefully find quadruplets summing to -1.
✗ Incorrect
The unique quadruplets summing to -1 are [-2, -1, 0, 2] and [-1, -1, 0, 1].
🚀 Application
expertCount Unique Quadruplets Summing to Target
Given the array nums = [1, 2, -2, -1, 0, 0, 1] and target = 1, how many unique quadruplets sum to the target?
Attempts:
2 left
💡 Hint
Use the Four Sum approach with duplicate skipping to find all unique quadruplets.
✗ Incorrect
The unique quadruplets summing to 1 are [ -2, 0, 1, 2 ], [ -1, 0, 0, 2 ], and [ -1, 0, 1, 1 ], totaling 3.
