Challenge - 5 Problems
Three Sum Mastery
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 Go function that finds all unique triplets in the array which gives the sum of zero?
DSA Go
package main import "fmt" func threeSum(nums []int) [][]int { var res [][]int n := len(nums) if n < 3 { return res } // Sort the array for i := 0; i < n-1; i++ { minIdx := i for j := i + 1; j < n; j++ { if nums[j] < nums[minIdx] { minIdx = j } } nums[i], nums[minIdx] = nums[minIdx], nums[i] } for i := 0; i < n-2; i++ { if i > 0 && nums[i] == nums[i-1] { continue } left, right := i+1, n-1 for left < right { sum := nums[i] + nums[left] + nums[right] if sum == 0 { res = append(res, []int{nums[i], nums[left], nums[right]}) left++ right-- for left < right && nums[left] == nums[left-1] { left++ } for left < right && nums[right] == nums[right+1] { right-- } } else if sum < 0 { left++ } else { right-- } } } return res } func main() { nums := []int{-1, 0, 1, 2, -1, -4} result := threeSum(nums) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about sorting the array first and then using two pointers to find pairs that sum with the current element to zero.
✗ Incorrect
The function sorts the array and then uses a fixed pointer with two moving pointers to find triplets that sum to zero. It skips duplicates to ensure unique triplets. The output for the input [-1, 0, 1, 2, -1, -4] is [[-1, -1, 2], [-1, 0, 1]].
🧠 Conceptual
intermediateNumber of Unique Triplets in Three Sum
Given the input array [-2, 0, 1, 1, 2], how many unique triplets sum to zero?
Attempts:
2 left
💡 Hint
Try to find all sets of three numbers that add up to zero without repeating the same triplet.
✗ Incorrect
The unique triplets are [-2, 0, 2] and [-2, 1, 1]. So there are 2 unique triplets.
🔧 Debug
advancedIdentify the Bug in Triplet Finding Code
What error will the following Go code produce when run, and why?
func threeSum(nums []int) [][]int {
var res [][]int
n := len(nums)
if n < 3 {
return res
}
// Missing sorting step
for i := 0; i < n-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
left, right := i+1, n-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
if sum == 0 {
res = append(res, []int{nums[i], nums[left], nums[right]})
left++
right--
for left < right && nums[left] == nums[left-1] {
left++
}
for left < right && nums[right] == nums[right+1] {
right--
}
} else if sum < 0 {
left++
} else {
right--
}
}
}
return res
}
func main() {
nums := []int{-1, 0, 1, 2, -1, -4}
result := threeSum(nums)
fmt.Println(result)
}
Attempts:
2 left
💡 Hint
Sorting is essential for skipping duplicates and using two pointers correctly.
✗ Incorrect
Without sorting, the two-pointer approach fails to find correct pairs and skip duplicates, resulting in incorrect output but no runtime error.
📝 Syntax
advancedIdentify Syntax Error in Three Sum Code
Which option contains a syntax error in the Go code snippet for three sum?
DSA Go
func threeSum(nums []int) [][]int { var res [][]int n := len(nums) if n < 3 { return res } sort.Ints(nums) for i := 0; i < n-2; i++ { if i > 0 && nums[i] == nums[i-1] { continue } left, right := i+1, n-1 for left < right { sum := nums[i] + nums[left] + nums[right] if sum == 0 { res = append(res, []int{nums[i], nums[left], nums[right]}) left++ right-- for left < right && nums[left] == nums[left-1] { left++ } for left < right && nums[right] == nums[right+1] { right-- } } else if sum < 0 { left++ } else { right-- } } } return res }
Attempts:
2 left
💡 Hint
Check if all used packages are imported.
✗ Incorrect
The code uses sort.Ints but does not import the "sort" package, causing a compile-time error.
🚀 Application
expertOptimizing Three Sum for Large Input
You have a very large array with millions of integers. Which approach is the best to optimize the Three Sum problem for unique triplets summing to zero?
Attempts:
2 left
💡 Hint
Consider time complexity and space usage for very large inputs.
✗ Incorrect
Sorting and two-pointer approach gives O(n^2) time with no extra space, which is efficient for large inputs compared to other methods.
