Time: O(n^3) because we use two nested loops and a two-pointer scan inside
Space: O(k) where k is number of quadruplets found, for storing results
vs Alternative: Better than naive O(n^4) which checks all quadruplets; sorting and two-pointer reduce complexity
Edge Cases
Empty array
Returns empty list, no quadruplets
DSA Typescript
for (let i = 0; i < n - 3; i++) {
Array with fewer than 4 elements
Returns empty list, no quadruplets possible
DSA Typescript
for (let i = 0; i < n - 3; i++) {
All elements are the same and sum matches target
Returns one quadruplet without duplicates
DSA Typescript
if (i > 0 && nums[i] === nums[i - 1]) continue;
When to Use This Pattern
When asked to find unique quadruplets summing to a target, use sorting plus two nested loops and two-pointer approach to efficiently find all combinations without duplicates.
Common Mistakes
Mistake: Not sorting the array before processing, causing duplicate quadruplets and inefficient search Fix: Sort the array first to enable skipping duplicates and two-pointer technique
Mistake: Not skipping duplicates for i, j, left, or right pointers, leading to repeated quadruplets Fix: Add checks to skip duplicates at each pointer position
Mistake: Moving pointers incorrectly after finding a quadruplet, causing infinite loops or missed results Fix: After recording quadruplet, move both pointers inward and skip duplicates carefully
Summary
Finds all unique sets of four numbers that add up to a target sum.
Use when you need to find quadruplets in an array that sum to a specific value without repeats.
Sort the array and use two nested loops plus two pointers to efficiently find quadruplets and skip duplicates.
Practice
(1/5)
1. What is the main goal of the Four Sum Problem in data structures and algorithms?
easy
A. Sort the array in ascending order
B. Find all unique quadruplets in an array that sum up to a target value
C. Find the maximum element in the array
D. Count the number of pairs that sum to a target
Solution
Step 1: Understand the problem statement
The Four Sum Problem asks to find all unique sets of four numbers in an array that add up to a given target sum.
Step 2: Identify the goal
The goal is to return all quadruplets without duplicates that sum to the target value.
Final Answer:
Find all unique quadruplets in an array that sum up to a target value -> Option B
Quick Check:
Four Sum Problem = Find quadruplets summing to target [OK]
Hint: Look for four numbers adding exactly to target [OK]
Common Mistakes:
Confusing with two sum or three sum problems
Ignoring uniqueness of quadruplets
Trying to find maximum or minimum instead of sum
2. Which of the following TypeScript function signatures correctly represents a function solving the Four Sum Problem?
easy
A. function fourSum(target: number, nums: number[]): void
B. function fourSum(nums: number): number
C. function fourSum(nums: string[], target: string): string[]
D. function fourSum(nums: number[], target: number): number[][]
Solution
Step 1: Analyze the function parameters and return type
The function should take an array of numbers and a target number, and return an array of quadruplets (arrays of numbers).
Step 2: Match the correct signature
function fourSum(nums: number[], target: number): number[][] matches this: input is number array and target number, output is array of number arrays.
Final Answer:
function fourSum(nums: number[], target: number): number[][] -> Option D