4Sum
Updated
Problem statement
Given an integer array nums and an integer target, return every unique quadruplet of values [a, b, c, d] whose elements come from four distinct indices and satisfy a + b + c + d = target.
Sort the values inside each quadruplet in nondecreasing order. Return the unique quadruplets in lexicographic order.
Function
fourSum(nums: int[], target: int) → List<List<Integer>>Examples
Example 1
nums = [1,0,-1,0,-2,2]target = 0return = [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]Pick four numbers from the list that add up to 0. There are three different sorted sets: [-2,-1,1,2], [-2,0,0,2] and [-1,0,0,1].
Example 2
nums = [2,2,2,2,2]target = 8return = [[2,2,2,2]]Any four of the five twos add up to the target. Many index choices give the same values, so [2,2,2,2] is listed once.
Example 3
nums = [1,2,3]target = 6return = []The list has only three numbers, so there is no way to pick four of them. The result is an empty array.
Constraints
0 <= nums.length <= 200.-10^9 <= nums[i] <= 10^9.-10^9 <= target <= 10^9.
Source note: Interview prompt excerpt preserved as source evidence.
More Amazon Onsite Interview questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dungeon GameONSITE INTERVIEW · Seen Nov 2026
- Integer BreakONSITE INTERVIEW · Seen Nov 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Next and Previous Lexicographic PermutationAdobe · ONSITE INTERVIEWAdobe interview questions
- Sort ColorsInfosys · ONSITE INTERVIEWInfosys interview questions
- Four SumPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences