Three Sum: Unique Target Triplets
Updated
Problem statement
Given an integer array nums and an integer target, return every distinct value triplet whose sum equals target.
- Sort the three values in each triplet from smallest to largest.
- The returned list must contain no duplicate value triplets.
- Return the triplets in lexicographic order.
You may reorder nums. Use 64-bit arithmetic for every three-value sum. The target complexity is O(n^2) time and O(1) auxiliary space, excluding the returned triplets and the sorting implementation's call stack.
Large-input follow-up
If the array cannot fit in memory, discuss how external sorting can produce an on-disk ordered sequence and how buffered scans trade additional I/O for bounded memory.
Function
threeSumTarget(nums: int[], target: int) → int[][]Examples
Example 1
nums = [-1,0,1,2,-1,-4]target = 0return = [[-1,-1,2],[-1,0,1]]The two distinct value triplets whose sum is 0 are [-1,-1,2] and [-1,0,1].
Example 2
nums = [2,2,2,2]target = 6return = [[2,2,2]]Many index triples exist, but they all produce the same value triplet, so [2,2,2] appears once.
Example 3
nums = [1,2,3,4,5,0,-1]target = 6return = [[-1,2,5],[-1,3,4],[0,1,5],[0,2,4],[1,2,3]]These are all five nondecreasing value triplets whose sum is 6, listed in lexicographic order.
Example 4
nums = [1,2,4,8]target = 100return = []No three values sum to 100.
Constraints
3 <= nums.length <= 3000.-10^9 <= nums[i] <= 10^9.-10^9 <= target <= 10^9.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 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
- 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