Challenge - 5 Problems
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Three Sum Unique Triplets Function
What is the output of the following Python function when called with nums = [-1, 0, 1, 2, -1, -4]?
DSA Python
def three_sum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return result print(three_sum([-1, 0, 1, 2, -1, -4]))
Attempts:
2 left
💡 Hint
Sort the list first and use two pointers to find pairs that sum with nums[i] to zero.
✗ Incorrect
The function sorts the list and uses a fixed pointer with two moving pointers to find all unique triplets that sum to zero. It skips duplicates to avoid repeated triplets.
❓ Predict Output
intermediate2:00remaining
Number of Unique Triplets Summing to Zero
How many unique triplets does the function find for the input nums = [3, 0, -2, -1, 1, 2]?
DSA Python
def three_sum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return result print(len(three_sum([3, 0, -2, -1, 1, 2])))
Attempts:
2 left
💡 Hint
Count the unique triplets after sorting and using two pointers.
✗ Incorrect
The unique triplets are [-2, -1, 3], [-2, 0, 2], [-1, 0, 1], so the count is 3.
🔧 Debug
advanced2:00remaining
Identify the Error in Triplet Finding Code
What best describes the result of running the following code with nums = [0, 0, 0, 0]?
DSA Python
def three_sum(nums): nums.sort() result = [] for i in range(len(nums) - 2): left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return result print(three_sum([0, 0, 0, 0]))
Attempts:
2 left
💡 Hint
Check how duplicates are handled inside the while loop.
✗ Incorrect
The code does not skip duplicates after finding a triplet nor in the outer loop, resulting in duplicate triplets [[0, 0, 0], [0, 0, 0]] in the output.
❓ Predict Output
advanced2:00remaining
Output of Modified Three Sum with Early Break
What is the output of this modified three_sum function when called with nums = [-2, 0, 1, 1, 2]?
DSA Python
def three_sum(nums): nums.sort() result = [] for i in range(len(nums) - 2): if nums[i] > 0: break if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left += 1 right -= 1 elif total < 0: left += 1 else: right -= 1 return result print(three_sum([-2, 0, 1, 1, 2]))
Attempts:
2 left
💡 Hint
The early break stops the loop if the current number is positive.
✗ Incorrect
The function finds two unique triplets that sum to zero: [-2, 0, 2] and [-2, 1, 1].
🧠 Conceptual
expert2:00remaining
Time Complexity of Three Sum Algorithm
What is the time complexity of the standard three sum algorithm that sorts the array and uses two pointers to find unique triplets?
Attempts:
2 left
💡 Hint
Consider the sorting step and the nested loops with two pointers.
✗ Incorrect
Sorting takes O(n log n). The main loop runs O(n) times, and inside it, the two pointers scan the array in O(n) total, resulting in O(n^2) overall.