FastPrepTwo or Three Sum Existence
Problems/Two or Three Sum Existence

Two or Three Sum Existence

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for a yes-or-no answer on whether two numbers sum to a target in sorted input, and extends it to three numbers. We set the rules for duplicates, signed values, and input limits. Our version is about 95% the same as the reported question.

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) → boolean

Examples

Example 1

nums = [1,2,4,7]target = 9k = 2return = true

The 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 = true

Select -2 and the two occurrences of 1 at different indices. Their sum is 0.

Example 3

nums = [3]target = 6k = 2return = false

The only index cannot be used twice, so there is no pair.

Example 4

nums = [1,2,3]target = 8k = 3return = false

The only three-element selection sums to 6, not 8.

Constraints

  • 0 <= nums.length <= 2000.
  • -2^31 <= nums[i], target <= 2^31 - 1.
  • nums is sorted in nondecreasing order, and repeated values are allowed.
  • k is 2 or 3.

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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean hasTargetSum(int[] nums, int target, int k) {
        // Write your code here.
    }

}
Checking account…