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 Quadruplets for a Simple Input
What is the output of the following Go code that finds all unique quadruplets summing to target 0?
DSA Go
package main import ( "fmt" "sort" ) func fourSum(nums []int, target int) [][]int { sort.Ints(nums) result := [][]int{} n := len(nums) for i := 0; i < n-3; i++ { if i > 0 && nums[i] == nums[i-1] { continue } for j := i + 1; j < n-2; j++ { if j > i+1 && nums[j] == nums[j-1] { continue } left, right := j+1, n-1 for left < right { sum := nums[i] + nums[j] + nums[left] + nums[right] if sum == target { result = append(result, []int{nums[i], nums[j], 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 < target { left++ } else { right-- } } } } return result } func main() { nums := []int{1, 0, -1, 0, -2, 2} target := 0 res := fourSum(nums, target) fmt.Println(res) }
Attempts:
2 left
💡 Hint
Think about sorting the array first and then using two pointers to find pairs.
✗ Incorrect
The code sorts the array and uses two nested loops with two pointers to find quadruplets that sum to the target 0. It skips duplicates to ensure unique quadruplets.
🧠 Conceptual
intermediateNumber of Unique Quadruplets for Given Input
Given the input array nums = [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, so quadruplets must use these repeated elements.
✗ Incorrect
Only one unique quadruplet [2, 2, 2, 2] sums to 8. The algorithm skips duplicates, so it counts this quadruplet once.
🔧 Debug
advancedIdentify the Bug in Four Sum Implementation
What error will the following code produce when run, and why?
func fourSum(nums []int, target int) [][]int {
sort.Ints(nums)
result := [][]int{}
n := len(nums)
for i := 0; i <= n-3; i++ {
for j := i + 1; j < n-2; j++ {
left, right := j+1, n-1
for left < right {
sum := nums[i] + nums[j] + nums[left] + nums[right]
if sum == target {
result = append(result, []int{nums[i], nums[j], nums[left], nums[right]})
left++
right--
} else if sum < target {
left++
} else {
right--
}
}
}
}
return result
}
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully for the outer loop.
✗ Incorrect
The outer loop uses i <= n-3 which allows i to be n-3, but inside the inner loops and pointer usage, this causes index out of range when accessing nums[i+3]. The correct condition is i < n-3.
📝 Syntax
advancedCorrect Syntax for Skipping Duplicates in Four Sum
Which option correctly skips duplicates for the second loop variable j in the four sum algorithm?
Attempts:
2 left
💡 Hint
The duplicate check for j must ensure it only skips when j is beyond the first iteration after i.
✗ Incorrect
The correct condition is j > i+1 to skip duplicates after the first j for each i. Other options either skip too early or too late.
🚀 Application
expertTime Complexity of Four Sum Algorithm
What is the time complexity of the standard four sum algorithm that sorts the array and uses two nested loops plus two pointers to find quadruplets?
Attempts:
2 left
💡 Hint
Consider the loops and the two-pointer approach inside the nested loops.
✗ Incorrect
The algorithm sorts the array in O(n log n), then uses two nested loops (O(n^2)) and inside uses two pointers moving linearly (O(n)), resulting in O(n^3) overall.
