Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, return the indices of the two numbers whose sum equals target.
- Use two different indices. Equal values at different indices are allowed.
- Exactly one unordered pair of indices is valid.
- Return the two zero-based indices in increasing order.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]The values at indices 0 and 1 are 2 and 7. Their sum is 9.
Example 2
nums = [3,2,4]target = 6return = [1,2]The values at indices 1 and 2 are 2 and 4. Their sum is 6.
Example 3
nums = [3,3]target = 6return = [0,1]The two values are equal, but they occupy different indices. Their sum is 6.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i], target <= 10^9- Exactly one pair of different indices has a sum equal to
target.
More IBM interview questions
- Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Nov 2026
- Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026
- Merge Two Sorted ListsPHONE SCREEN · Seen Oct 2026
- Reverse a Linked ListPHONE SCREEN · Seen Oct 2026
- Variadic Function Library MatchesPHONE SCREEN · Seen Sep 2026
- Count Active Employee Access GrantsPHONE SCREEN · Seen Sep 2026
- Count Triplets Within a Value RangePHONE SCREEN · Seen Sep 2026
- Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
- Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
Similar questions at other companies
- 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
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions