Two Sum
Updated
Problem statement
Find the two positions in nums whose values add up to target. There is always exactly one such pair of positions i < j. The two positions must be different, but their values may be equal.
Return the pair as [i, j], smaller index first. Indexes start at 0.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [31,-12,7,44,-5,19]target = 39return = [3,4]44 + (-5) = 39, and 44 and -5 are at indexes 3 and 4. Every other pair gives a different total, for example 31 + 7 = 38 and 7 + 19 = 26.
Example 2
nums = [10,4,6,6,1,9]target = 12return = [2,3]Slots 2 and 3 each hold a 6, and 6 + 6 = 12. They are separate slots of the array, so taking both is fine, even though the values match.
Example 3
nums = [12,-8,30,4,-25]target = -33return = [1,4]-8 + (-25) = -33, using indexes 1 and 4. Every pair that includes 12, 30 or 4 adds up to more than -33.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9- Exactly one pair of indexes
i < jhasnums[i] + nums[j] == target.
More Capital One Online Assessment questions
- Branch with the Most FilesOA · Seen Nov 2026
- Earliest Meeting Time for All EmployeesOA · Seen Nov 2026
- Earn the Most 🐝OA · Seen Nov 2026
- Rank Football TeamsOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
- Arithmetic Triplets After QueriesOA · Seen Oct 2026
- Count One-Swap Number PairsOA · Seen Oct 2026
- Distinct Values on Maximum-Sum FramesOA · Seen Oct 2026
- Sort Adjacent PairsOA · Seen Oct 2026
- Bouncing Diagonal WeightsOA · Seen Oct 2026
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
- 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
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences