Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, return two different indices whose values add up to target.
For this exercise, assume exactly one unordered pair of distinct indices satisfies the target. Return the smaller index first. The array is not necessarily sorted, and you cannot use one element twice.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [4,1,7,6]target = 10return = [0,3]The values at indices 0 and 3 are 4 and 6, which sum to 10.
Example 2
nums = [5,5]target = 10return = [0,1]Equal values can form the answer when they occupy two different indices.
Example 3
nums = [-3,4,9,-1]target = 8return = [2,3]The values 9 and -1 sum to 8; return their original indices.
Constraints
- For this exercise, assume
2 <= nums.length <= 10^5. - For this exercise, assume
-10^9 <= nums[i], target <= 10^9.
More Vanguard Online Assessment questions
- Valid ParenthesesOA · Seen Sep 2026
- Are They PangramsOA · Seen Nov 2025
- Missing WordsOA · Seen Nov 2025
- Spam DetectionOA · Seen Nov 2025
- Binge WatchingOA · Seen Jan 2023
- Minimum Moves Around a Circular ArrayOA · Seen Aug 2022
- Maximum Occurring CharacterOA · Seen Mar 2022
- Picking TicketsOA · Seen Mar 2022
- Tool List: Two Smallest ValuesOA · Seen Nov 2021
- Group AnagramsOA · Seen Nov 2019
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Count Valid Square PairsAuxoAI · OA
- Two SumAmazon · OA · PHONE SCREEN
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Customer Signer ScreeningState Farm · OA
- Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
- K Most Recent Unique Request IDsMicrosoft · OAMicrosoft interview questions
- Distinct Products After Removing One ElementHSBC · OAHSBC interview questions