Two Sum with an Ordered Index Pair
Updated
Problem statement
Given an integer array nums and an integer target, return two distinct indices [i,j] with i < j such that nums[i] + nums[j] == target.
If several pairs work, return the one with the smallest first index. If those first indices are equal, pick the smallest second index. Return an empty array when no pair works, including when nums has fewer than two numbers.
Repeated values may be used if they sit at different indices. Do not reorder or modify the input array.
Function
twoSumPair(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]Indices 0 and 1 contain 2 and 7, whose sum is 9.
Example 2
nums = [3,3,3]target = 6return = [0,1]Any two of the three equal values add up to the target, so all three pairs work. [0,1] wins because both of its indices are the smallest.
Example 3
nums = [1,4,6,9]target = 10return = [0,3]Both [0,3] and [1,2] sum to 10. Choose [0,3] because its first index is smaller.
Constraints
0 <= nums.length <= 500.-10^9 <= nums[i] <= 10^9.-2 * 10^9 <= target <= 2 * 10^9.
More Atlassian Phone Screen questions
- Merge Overlapping IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Tennis Court SchedulingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Longest Consistent Activity StreakPHONE SCREEN · Seen Sep 2026
- Music Player Operation SequencePHONE SCREEN · Seen Sep 2026
- Underline Shortest Unique SubstringsPHONE SCREEN · Seen Aug 2026
- Can a Dictionary Word Be Formed from Note Letters?PHONE SCREEN · Seen Feb 2026
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Feb 2026
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Aug 2025
- Can You Finish the Teleporter Board?PHONE SCREEN · Seen Jul 2025
Similar questions at other companies
- 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
- Two SumBNSF Railway · PHONE SCREENBNSF Railway interview questions