Find Two-Sum or Three-Sum Index Combinations
Updated
Problem statement
Given an integer array nums, an integer target, and tupleSize equal to 2 or 3, return every index combination whose values sum to target.
- For
tupleSize = 2, return every[i, j]withi < jandnums[i] + nums[j] = target. - For
tupleSize = 3, return every[i, j, k]withi < j < kandnums[i] + nums[j] + nums[k] = target.
Each distinct index combination appears once, even when values repeat. Return combinations in lexicographic index order.
Function
findTargetIndexCombinations(nums: int[], target: int, tupleSize: int) → int[][]Examples
Example 1
nums = [2,7,11,15]target = 9tupleSize = 2return = [[0,1]]Indices 0 and 1 contain 2 and 7, which sum to 9.
Example 2
nums = [-1,0,1,2,-1,-4]target = 0tupleSize = 3return = [[0,1,2],[0,3,4],[1,2,4]]The repeated value -1 occurs at different indices, so all three distinct index triples are retained.
Example 3
nums = [0,0,0,0]target = 0tupleSize = 2return = [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]All six pairs of different indices are valid and appear in lexicographic order.
Constraints
2 <= nums.length <= 200-1000000 <= nums[i], target <= 1000000tupleSizeis 2 or 3 and does not exceednums.length.- The number of returned combinations is at most 200000.
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
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Repeated Shortest Word Distance QueriesLinkedIn · PHONE SCREENLinkedIn interview questions
- Merge Keyed 2D ArraysWhatNot · PHONE SCREENWhatNot interview questions
- Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions
- Efficient TeamsExpedia · OAExpedia interview questions
- Sparse Vector Dot ProductAnduril · PHONE SCREENAnduril interview questions
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN