Four Sum
Updated
Problem statement
You are given an integer array nums and a target. Find every group of four values whose sum equals target. The four values must come from four different array positions.
For this exercise, sort the four values in each group from smallest to largest, and sort the result list lexicographically. Return each group of values only once, even if several choices of positions produce it. Return an empty list if there is no such group. You may change the input array.
Function
fourSum(nums: int[], target: long) → int[][]Examples
Example 1
nums = [1,0,-1,0,-2,2]target = 0return = [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]These are the three distinct sorted value quadruplets with sum zero. Their indices are distinct within each selection.
Example 2
nums = [2,2,2,2,2]target = 8return = [[2,2,2,2]]Several index selections exist, but all produce the same value quadruplet, which appears once.
Constraints
- For this exercise, assume
0 <= nums.length <= 200. -10^9 <= nums[i] <= 10^9.-4 * 10^9 <= target <= 4 * 10^9; use 64-bit sums.
More PeopleStrong Onsite Interview questions
- Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Mar 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Mar 2026
- Find the Entry of a Linked List CycleONSITE INTERVIEW · Seen Sep 2025
- Integer to English Words: Up to 20 DigitsONSITE INTERVIEW · Seen Sep 2025
- Word Ladder II: All Shortest TransformationsONSITE INTERVIEW · Seen Sep 2025
- Detect a Cycle in a Linked ListONSITE INTERVIEW · Seen May 2025
- First Nonrepeating CharacterONSITE INTERVIEW · Seen May 2025
- Longest Subarray with a Target SumONSITE INTERVIEW · Seen May 2025
- Lowest Common Ancestor in a BSTONSITE INTERVIEW · Seen May 2025
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
- 4SumAmazon · ONSITE INTERVIEW
- 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