Two Sum
Updated
Problem statement
Given an integer array nums and an integer target, return two distinct zero-based indices whose values sum to target.
For this exercise, assume exactly one pair of indices i < j satisfies the sum. Return [i,j] in increasing index order. The same array position cannot be used twice.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [7,4,11,2]target = 9return = [0,3]The values at indices 0 and 3 are 7 and 2, and 7 + 2 = 9. No other distinct-index pair has that sum.
Example 2
nums = [-3,10,5,1]target = 2return = [0,2]The pair at indices 0 and 2 gives -3 + 5 = 2. Its indices are already in increasing order.
Constraints
- For this exercise, assume 2 <= nums.length <= 2000.
- For this exercise, assume -1000000 <= nums[i] <= 1000000.
- For this exercise, assume -2000000 <= target <= 2000000.
- Exactly one pair of distinct indices i < j satisfies nums[i] + nums[j] = target.
More Morgan Stanley interview questions
- Unique Grid PathsPHONE SCREEN · Seen Oct 2026
- Reverse an IntegerPHONE SCREEN · Seen Jul 2024
- Second and Third Distinct Order ValuesPHONE SCREEN · Seen Jul 2024
- Binary Tree Preorder TraversalPHONE SCREEN · Seen Jan 2024
- Find a Target-Sum SubarrayPHONE SCREEN · Seen Jan 2024
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
Similar questions at other companies
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Two SumBNSF Railway · PHONE SCREENBNSF Railway interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)