Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, return the two distinct indices whose values add up to target.
Exactly one valid pair exists. Return the smaller index first.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]nums[0] + nums[1] = 2 + 7 = 9.
Example 2
nums = [3,2,4]target = 6return = [1,2]nums[1] + nums[2] = 2 + 4 = 6.
Example 3
nums = [3,3]target = 6return = [0,1]The two equal values occur at distinct indices.
Constraints
2 <= nums.length <= 10000.-1000000000 <= nums[i], target <= 1000000000.- Exactly one valid answer exists.
Source note: Author-written prompt excerpt retained for source fidelity.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Count Distinct Pairs With Sum KeBay · ONSITE INTERVIEWeBay interview questions
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Price CheckBlue Origin · PHONE SCREEN
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN