Challenge - 5 Problems
Four Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Four Sum with Small Array
What is the output of the following Java code that finds all unique quadruplets summing to target 0?
DSA Java
import java.util.*; public class FourSum { public static List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); 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(Arrays.asList(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; } public static void main(String[] args) { int[] nums = {1, 0, -1, 0, -2, 2}; List<List<Integer>> result = fourSum(nums, 0); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about sorting and skipping 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 uniqueness. The correct quadruplets are [-2, -1, 1, 2], [-2, 0, 0, 2], and [-1, 0, 0, 1].
🧠 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 prevents adding the same quadruplet more than once, ensuring the result contains only unique quadruplets.
🔧 Debug
advancedIdentify the Error in Four Sum Implementation
What error will the following code produce when run?
DSA Java
import java.util.*; public class FourSumBug { public static List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); 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(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); left++; right--; } else if (sum < target) { left++; } else { right--; } } } } return res; } }
Attempts:
2 left
💡 Hint
Check the loop conditions carefully for array boundaries.
✗ Incorrect
The outer loop runs with i <= n - 3, which allows i to be n-3, but inside the inner loops and pointers, this causes index out of bounds when accessing nums[j] or pointers.
🚀 Application
advancedNumber of Unique Quadruplets for Large Input
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
Consider how duplicates are handled and the quadruplet sums.
✗ Incorrect
All elements are 2, so the only quadruplet summing to 8 is [2, 2, 2, 2]. The algorithm returns only one unique quadruplet.
❓ Predict Output
expertOutput of Four Sum with Negative and Positive Numbers
What is the output of this Java code snippet that finds all unique quadruplets summing to 2?
DSA Java
import java.util.*; public class FourSumExpert { public static List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); 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(Arrays.asList(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; } public static void main(String[] args) { int[] nums = {-3, -1, 0, 2, 4, 5}; List<List<Integer>> result = fourSum(nums, 2); System.out.println(result); } }
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 contain numbers not in the array or sums that don't match.
