Two or Three Sum Existence
Updated
Problem statement
You are given an integer array nums, sorted from smallest to largest, an integer target, and k, which is 2 or 3. Check whether exactly k elements from different array positions add up to target.
Return true if such a selection exists and false otherwise. Equal values at different indices may both be selected, but an index cannot be reused. If the array has fewer than k elements, return false.
Values may be negative, zero, or positive. Compute sums mathematically, without signed 32-bit overflow; use a wider integer type when necessary.
Interview progression
The first part asks whether a pair exists. The follow-up changes the selection to three elements while retaining sorted input. The parameter k selects the part to solve.
Unsorted-input follow-up
Discuss how the approach changes if the array is unsorted. Sorting a copy first allows the same two-pointer reasoning afterward. The input to your function is already sorted.
Function
hasTargetSum(nums: int[], target: int, k: int) → booleanExamples
Example 1
nums = [1,2,4,7]target = 9k = 2return = trueThe values at indices 1 and 3 are 2 and 7, whose sum is 9.
Example 2
nums = [-2,0,1,1,4]target = 0k = 3return = trueSelect -2 and the two occurrences of 1 at different indices. Their sum is 0.
Example 3
nums = [3]target = 6k = 2return = falseThe only index cannot be used twice, so there is no pair.
Example 4
nums = [1,2,3]target = 8k = 3return = falseThe only three-element selection sums to 6, not 8.
Constraints
0 <= nums.length <= 2000.-2^31 <= nums[i], target <= 2^31 - 1.numsis sorted in nondecreasing order, and repeated values are allowed.kis2or3.
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
- Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle 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
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Rotate an Array Right by K StepsQualys · ONSITE INTERVIEWQualys interview questions