Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, find the two different indices i and j such that nums[i] + nums[j] == target.
Each element can be used at most once, so an index cannot be paired with itself. Two different indices may hold the same value. Exactly one valid pair exists. Return it as [i, j] with the smaller index first.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [4,9,1,6]target = 7return = [2,3]nums[2] + nums[3] = 1 + 6 = 7, so the answer is [2, 3]. No other pair adds up to 7.
Example 2
nums = [5,-2,8,5]target = 10return = [0,3]The two 5s are different elements at indices 0 and 3, and 5 + 5 = 10. The smaller index comes first, so the answer is [0, 3].
Example 3
nums = [5,3,7]target = 10return = [1,2]5 + 5 would be 10, but there is only one 5 and an element cannot be used twice. The pair is 3 + 7 = 10 at indices 1 and 2.
Constraints
2 <= nums.length <= 10^4-10^9 <= nums[i] <= 10^9-10^9 <= target <= 10^9- Exactly one pair of indices
i < jhasnums[i] + nums[j] == target.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
- Disjoint Target-Sum TripletsAmerican Express · PHONE SCREENAmerican Express interview questions
- Efficient TeamsExpedia · OAExpedia interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)