Three Sum: Unique Zero-Sum Triplets
Updated
Problem statement
Given an integer array nums, return every distinct value triplet whose elements come from three distinct indices and whose sum is 0.
- Each triplet must be nondecreasing.
- The returned list must contain no duplicate value triplets.
- Return the triplets in lexicographic order.
Use 64-bit arithmetic when computing sums.
Function
threeSum(nums: int[]) → List<List<Integer>>Examples
Example 1
nums = [-1,0,1,2,-1,-4]return = [[-1,-1,2],[-1,0,1]]The two distinct value triplets with sum 0 are [-1,-1,2] and [-1,0,1].
Example 2
nums = [0,0,0,0]return = [[0,0,0]]Many index triples exist, but they all produce the same value triplet, so [0,0,0] appears once.
Example 3
nums = [1,2,-2,-1]return = []No three values sum to 0.
Constraints
3 <= nums.length <= 3000.-10^9 <= nums[i] <= 10^9.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- 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
- 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