FastPrepTwo Sum
Problems/Two Sum

Two Sum

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A review of a Microsoft software engineering internship hire lists Two Sum by name for its coding interview with engineers.

Nothing else about the task is reported. Here you return the two indices, smaller one first, of the only pair whose values add up to the target. An element cannot be used twice. There are 2 to 10^4 numbers, and every number and the target are between -10^9 and 10^9.

Our version is about 95% the same as the reported question.

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 < j has nums[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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[] twoSum(int[] nums, int target) {
        // Write your code here.
    }
}
Checking account…