0
0
DSA Pythonprogramming~20 mins

Four Sum Problem All Unique Quadruplets in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Four Sum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Four Sum Unique Quadruplets Function
What is the output of the following Python function when called with nums = [1, 0, -1, 0, -2, 2] and target = 0?
DSA Python
def four_sum(nums, target):
    nums.sort()
    quadruplets = []
    n = len(nums)
    for i in range(n - 3):
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        for j in range(i + 1, n - 2):
            if j > i + 1 and nums[j] == nums[j - 1]:
                continue
            left, right = j + 1, n - 1
            while left < right:
                total = nums[i] + nums[j] + nums[left] + nums[right]
                if total == target:
                    quadruplets.append([nums[i], nums[j], nums[left], nums[right]])
                    left += 1
                    right -= 1
                    while left < right and nums[left] == nums[left - 1]:
                        left += 1
                    while left < right and nums[right] == nums[right + 1]:
                        right -= 1
                elif total < target:
                    left += 1
                else:
                    right -= 1
    return quadruplets

result = four_sum([1, 0, -1, 0, -2, 2], 0)
print(result)
A[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
B[[-2, -1, 0, 1], [-1, 0, 0, 1], [-2, 0, 0, 2]]
C[[-2, -1, 1, 2], [-1, 0, 0, 1]]
D[[-2, -2, 0, 2], [-1, 0, 0, 1], [-2, 0, 0, 2]]
Attempts:
2 left
💡 Hint
Think about sorting and skipping duplicates to find unique quadruplets.
🧠 Conceptual
intermediate
1:00remaining
Number of Unique Quadruplets for Four Sum
Given the list nums = [2, 2, 2, 2, 2] and target = 8, how many unique quadruplets does the four sum algorithm find?
A1
B5
C0
D10
Attempts:
2 left
💡 Hint
All elements are the same, so quadruplets must be identical.
🔧 Debug
advanced
1:30remaining
Identify the Error in Four Sum Implementation
What error will occur when running this code snippet for four sum?
DSA Python
def four_sum_bug(nums, target):
    nums.sort()
    quadruplets = []
    n = len(nums)
    for i in range(n - 3):
        for j in range(i + 1, n - 2):
            left, right = j + 1, n - 1
            while left < right:
                total = nums[i] + nums[j] + nums[left] + nums[right]
                if total == target:
                    quadruplets.append([nums[i], nums[j], nums[left], nums[right]])
                    left += 1
                    right -= 1
                elif total < target:
                    left += 1
                else:
                    right -= 1
    return quadruplets

print(four_sum_bug([1, 0, -1, 0, -2, 2], 0))
AIndexError due to out of range access
BNo error, but duplicates quadruplets will appear
CTypeError because of wrong data type
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Check if duplicates are handled in the loops.
🚀 Application
advanced
2:00remaining
Find Quadruplets Summing to Target in Large Array
Given nums = [1, 0, -1, 0, -2, 2, 2, -1, -4] and target = 0, which of the following lists contains all unique quadruplets returned by the four sum algorithm?
A[[-4, -2, 2, 4], [-2, -1, 1, 2], [-1, 0, 0, 1]]
B[[-4, -1, 1, 2], [-2, -1, 1, 2], [-1, 0, 0, 1], [-2, 0, 0, 2]]
C[[-4, -2, 2, 4], [-2, 0, 0, 2], [-1, 0, 0, 1]]
D[[-4, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
Attempts:
2 left
💡 Hint
Sort the array and carefully find quadruplets summing to zero without duplicates.
🧠 Conceptual
expert
1:00remaining
Time Complexity of Four Sum Algorithm
What is the time complexity of the standard four sum algorithm that finds all unique quadruplets in an array of length n?
AO(n^2)
BO(n log n)
CO(n^3)
DO(n^4)
Attempts:
2 left
💡 Hint
Consider nested loops and two-pointer approach inside.