Two Sum
Updated
Problem statement
Given nums and target, return the two distinct indices whose values add to target.
Exactly one valid pair exists. Return [i, j] with zero-based indices and i < j. The array is not necessarily sorted, and an element cannot be used twice.
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 sum to 6. The single value 3 cannot be reused.
Example 3
nums = [3,3]target = 6return = [0,1]The equal values occupy different indices, so indices 0 and 1 form a valid pair.
Constraints
- 2 <= nums.length <= 100000.
- Every value in nums is between -1000000 and 1000000, inclusive.
- The target is between -2000000 and 2000000, inclusive.
- Exactly one pair of distinct indices has values summing to target.
More London Stock Exchange interview questions
- Implement a Queue Using an ArrayPHONE SCREEN · Seen Feb 2020
- Lowest Common Ancestor in a Binary TreePHONE SCREEN · Seen Feb 2020
- Palindrome PartitioningPHONE SCREEN · Seen Feb 2020
- Two Sum in a Sorted ArrayPHONE SCREEN · Seen Feb 2020
- Group Anagram StringsONSITE INTERVIEW · Seen Apr 2020
- Implement Key Equality and HashingONSITE INTERVIEW · Seen Apr 2020
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