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 Quadruplets for a Given Array
What is the output of the following Rust code that finds all unique quadruplets in the array that sum to the target 0?
DSA Rust
fn four_sum(nums: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
let n = nums.len();
for i in 0..n {
if i > 0 && nums[i] == nums[i - 1] { continue; }
for j in i + 1..n {
if j > i + 1 && nums[j] == nums[j - 1] { continue; }
let mut left = j + 1;
let mut right = n - 1;
while left < right {
let sum = nums[i] + nums[j] + nums[left] + nums[right];
if sum == target {
res.push(vec![nums[i], nums[j], nums[left], nums[right]]);
while left < right && nums[left] == nums[left + 1] { left += 1; }
while left < right && nums[right] == nums[right - 1] { right -= 1; }
left += 1;
right -= 1;
} else if sum < target {
left += 1;
} else {
right -= 1;
}
}
}
}
res
}
fn main() {
let nums = vec![1, 0, -1, 0, -2, 2];
let target = 0;
let result = four_sum(nums, target);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember to sort the array and skip duplicates to find unique quadruplets.
✗ Incorrect
The code sorts the array and uses two nested loops with two pointers to find quadruplets summing to the target. It skips duplicates to ensure unique quadruplets. The correct output matches option C.
🧠 Conceptual
intermediateNumber of Unique Quadruplets for a Given Input
Given the array [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; consider how many unique quadruplets can be formed.
✗ Incorrect
Since all elements are 2, the only quadruplet summing to 8 is [2, 2, 2, 2]. The algorithm returns one unique quadruplet.
🔧 Debug
advancedIdentify the Error in Quadruplet Search Code
What error will the following Rust code produce when run?
DSA Rust
fn four_sum(nums: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
let n = nums.len();
for i in 0..n {
for j in i + 1..n {
let mut left = j + 1;
let mut right = n - 1;
while left < right {
let sum = nums[i] + nums[j] + nums[left] + nums[right];
if sum == target {
res.push(vec![nums[i], nums[j], nums[left], nums[right]]);
left += 1;
right -= 1;
} else if sum < target {
left += 1;
} else {
right -= 1;
}
}
}
}
res
}
fn main() {
let nums = vec![1, 0, -1, 0, -2, 2];
let target = 0;
let result = four_sum(nums, target);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check how the code handles duplicate skipping and index boundaries.
✗ Incorrect
The code does not skip duplicates and does not check if left+1 or right-1 are valid before incrementing/decrementing, causing potential index out of bounds panic.
🚀 Application
advancedFind Quadruplets Summing to Target in Large Array
Given a sorted array of 10,000 integers ranging from -5000 to 5000, which approach is best to find all unique quadruplets that sum to a target value efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity and how sorting helps two pointers.
✗ Incorrect
Two nested loops combined with two pointers after sorting gives O(n^3) time, which is more efficient than four nested loops (O(n^4)) and simpler than hash map or binary search approaches for this problem.
❓ Predict Output
expertOutput of Four Sum with Negative and Positive Numbers
What is the output of the following Rust code snippet?
DSA Rust
fn four_sum(nums: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
let n = nums.len();
for i in 0..n {
if i > 0 && nums[i] == nums[i - 1] { continue; }
for j in i + 1..n {
if j > i + 1 && nums[j] == nums[j - 1] { continue; }
let mut left = j + 1;
let mut right = n - 1;
while left < right {
let sum = nums[i] + nums[j] + nums[left] + nums[right];
if sum == target {
res.push(vec![nums[i], nums[j], nums[left], nums[right]]);
while left < right && nums[left] == nums[left + 1] { left += 1; }
while left < right && nums[right] == nums[right - 1] { right -= 1; }
left += 1;
right -= 1;
} else if sum < target {
left += 1;
} else {
right -= 1;
}
}
}
}
res
}
fn main() {
let nums = vec![-3, -1, 0, 2, 4, 5];
let target = 2;
let result = four_sum(nums, target);
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 do not match target.
