📖 Scenario: You are working on a shopping app that wants to find all unique groups of four products whose prices add up to a special discount target. This helps the app suggest bundles to customers.
🎯 Goal: Build a TypeScript program that finds all unique quadruplets (groups of four numbers) in an array that sum up to a given target value.
📋 What You'll Learn
Create an array called prices with the exact values: [1, 0, -1, 0, -2, 2]
Create a variable called target and set it to 0
Write a function called fourSum that takes nums: number[] and target: number and returns all unique quadruplets as an array of arrays
Use sorting and two-pointer technique inside fourSum to find quadruplets
Print the result of calling fourSum(prices, target)
💡 Why This Matters
🌍 Real World
Finding groups of items that meet a specific sum is useful in shopping apps, budgeting tools, and financial analysis.
💼 Career
This problem teaches array manipulation, sorting, and two-pointer techniques commonly asked in software engineering interviews.
Progress0 / 4 steps
1
Create the initial array of prices
Create an array called prices with these exact values: [1, 0, -1, 0, -2, 2]
DSA Typescript
Hint
Use const prices = [1, 0, -1, 0, -2, 2]; to create the array.
2
Set the target sum value
Create a variable called target and set it to 0
DSA Typescript
Hint
Use const target = 0; to set the target sum.
3
Write the fourSum function to find unique quadruplets
Write a function called fourSum that takes parameters nums: number[] and target: number. Inside, sort nums, then use nested loops and two pointers to find all unique quadruplets that sum to target. Return the array of quadruplets.
DSA Typescript
Hint
Sort the array first. Use two nested loops for the first two numbers, then two pointers for the last two numbers. Skip duplicates to keep quadruplets unique.
4
Print the result of fourSum with prices and target
Print the result of calling fourSum(prices, target) using console.log
DSA Typescript
Hint
Use console.log(fourSum(prices, target)); to print the result.
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