Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, find two distinct indices whose values add to target.
Return a two-element array [i, j], where 0 <= i < j < nums.length and nums[i] + nums[j] = target. At least one valid pair exists. If several pairs are valid, return the lexicographically smallest pair: minimize i first, then j.
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 occupy distinct indices and form the required pair.
Constraints
2 <= nums.length <= 100000.-10^9 <= nums[i] <= 10^9.-10^9 <= target <= 10^9.- At least one valid pair exists.
More Amazon Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Build the Task HierarchyPendo · OA
- Price CheckBlue Origin · PHONE SCREEN
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Count User Mentions in Chat MessagesRamp · OARamp 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
- Count Valid Square PairsAuxoAI · OA
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions