0
0
DSA Pythonprogramming~20 mins

Three Sum Problem All Unique Triplets in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]))
A[[-4, 2, 2], [-1, 0, 1]]
B[[-1, -1, 2], [-1, 0, 1]]
C[[-1, 0, 1], [0, 1, -1]]
D[[-1, -1, 2], [-4, 0, 4]]
Attempts:
2 left
💡 Hint
Sort the list first and use two pointers to find pairs that sum with nums[i] to zero.
Predict Output
intermediate
2: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])))
A3
B1
C4
D2
Attempts:
2 left
💡 Hint
Count the unique triplets after sorting and using two pointers.
🔧 Debug
advanced
2: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]))
ANo error, output: [[0, 0, 0], [0, 0, 0]]
BInfinite loop because duplicates are not skipped
CIndexError due to skipping duplicates incorrectly
DTypeError due to wrong data type
Attempts:
2 left
💡 Hint
Check how duplicates are handled inside the while loop.
Predict Output
advanced
2: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]))
A[[0, 1, -1]]
B[[-2, 1, 1]]
C[[-2, 0, 2], [-2, 1, 1]]
D[]
Attempts:
2 left
💡 Hint
The early break stops the loop if the current number is positive.
🧠 Conceptual
expert
2: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?
AO(n^3)
BO(n log n)
CO(n)
DO(n^2)
Attempts:
2 left
💡 Hint
Consider the sorting step and the nested loops with two pointers.