Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, return the indices of the two numbers that add up to target.
Exactly one pair of indices works. You may not use the same element twice, but two different elements may hold the same value. Indices start at 0. Return the two indices in increasing order.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [3,8,12,5]target = 8return = [0,3]nums[0] + nums[3] = 3 + 5 = 8. No other pair adds up to 8; the 8 at index 1 would need a 0.
Example 2
nums = [5,2,8]target = 10return = [1,2]nums[1] + nums[2] = 2 + 8 = 10. The 5 would need another 5, and it cannot be paired with itself.
Example 3
nums = [4,2,9,4]target = 8return = [0,3]The two 4s are different elements, so nums[0] + nums[3] = 4 + 4 = 8 is allowed.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9- Exactly one valid answer exists.
More Shnoor International Onsite Interview questions
Similar questions at other companies
- Best Average Student ScoreGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Count Distinct Pairs With Sum KeBay · ONSITE INTERVIEWeBay interview questions
- List the Key and Value of Every Map EntryNewgen Software · ONSITE INTERVIEW
- Pizza Price CalculatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- Longest Dictionary Word from Nine LettersGoogle · ONSITE INTERVIEWGoogle interview questions
- Two SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Keep the First Key for Each Distinct ValueOneTrust · ONSITE INTERVIEW