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 Rust function when called with input [-1, 0, 1, 2, -1, -4]?
DSA Rust
fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
for i in 0..nums.len() {
if i > 0 && nums[i] == nums[i - 1] {
continue;
}
let mut left = i + 1;
let mut right = nums.len() - 1;
while left < right {
let sum = nums[i] + nums[left] + nums[right];
if sum == 0 {
res.push(vec![nums[i], nums[left], nums[right]]);
while left + 1 < nums.len() && left < right && nums[left] == nums[left + 1] {
left += 1;
}
while right > 0 && left < right && nums[right] == nums[right - 1] {
right -= 1;
}
left += 1;
right -= 1;
} else if sum < 0 {
left += 1;
} else {
right -= 1;
}
}
}
res
}
fn main() {
let input = vec![-1, 0, 1, 2, -1, -4];
let result = three_sum(input);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Sort the array first and use two pointers to find pairs that sum with nums[i] to zero.
✗ Incorrect
The function sorts the array and uses a fixed pointer i and two pointers left and right to find triplets that sum to zero. It skips duplicates to ensure unique triplets. For the input [-1, 0, 1, 2, -1, -4], the unique triplets are [-1, -1, 2] and [-1, 0, 1].
🧠 Conceptual
intermediateNumber of Unique Triplets in Three Sum
Given the input array [3, -2, 1, 0, -1, 2, -3], how many unique triplets sum to zero?
Attempts:
2 left
💡 Hint
Try sorting and then find triplets using two pointers.
✗ Incorrect
The unique triplets that sum to zero are [-3,1,2], [-2,0,2], and [-1,0,1]. Other suggested triplets include elements not in the array or duplicates. So total 3 unique triplets.
🔧 Debug
advancedIdentify the Bug in Three Sum Implementation
What error will this Rust code produce when run with input [0, 0, 0, 0]?
DSA Rust
fn three_sum(nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut nums = nums;
nums.sort();
let mut res = Vec::new();
for i in 0..nums.len() {
if i > 0 && nums[i] == nums[i - 1] {
continue;
}
let mut left = i + 1;
let mut right = nums.len() - 1;
while left < right {
let sum = nums[i] + nums[left] + nums[right];
if sum == 0 {
res.push(vec![nums[i], 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 < 0 {
left += 1;
} else {
right -= 1;
}
}
}
res
}
fn main() {
let input = vec![0, 0, 0, 0];
let result = three_sum(input);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check the conditions inside the while loops that skip duplicates.
✗ Incorrect
When left is at the second last index, nums[left + 1] accesses out of bounds causing a panic. The condition should ensure left + 1 < nums.len() before accessing nums[left + 1].
📝 Syntax
advancedCorrect Syntax for Skipping Duplicates in Three Sum
Which option correctly fixes the syntax error in this Rust snippet for skipping duplicates?
{ x: x*2 if x > 2 for x in 0..5 }
Attempts:
2 left
💡 Hint
Rust uses iterators with filter and map, not Python-style comprehensions.
✗ Incorrect
Option B uses Rust iterator methods filter and map correctly and collects into a vector. Option B is invalid syntax. Option B returns Option due to if without else, causing type mismatch. Option B misses collect(), so result is an iterator, not a vector.
🚀 Application
expertOptimizing Three Sum for Large Inputs
Which approach best optimizes the Three Sum problem for very large input arrays to reduce time complexity?
Attempts:
2 left
💡 Hint
Consider the best known time complexity for Three Sum and practical approaches.
✗ Incorrect
The optimal known approach sorts the array and uses two pointers for each element, resulting in O(n^2) time. Three nested loops are slower (O(n^3)). Using hash sets for complements is tricky for triplets and usually not better than O(n^2). Binary search adds log n factor, making it slower than two pointers.
