Two Sum
Updated
Problem statement
Return two distinct zero-based indices [i, j] such that i < j and nums[i] + nums[j] = target.
If several pairs work, choose the pair with the smallest j, then the smallest i. If no pair works, return [-1, -1].
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [2,7,11,15]target = 9return = [0,1]The values at indices 0 and 1 are 2 and 7, whose sum is 9.
Example 2
nums = [3,3]target = 6return = [0,1]The two separate occurrences of 3 add to 6; a single index cannot be reused.
Constraints
0 <= nums.length <= 10000-10^6 <= nums[i] <= 10^6-2 * 10^6 <= target <= 2 * 10^6
More JP Morgan Chase Onsite Interview questions
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU CacheONSITE INTERVIEW · Seen Nov 2026
- Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
- Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
- K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
- Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
- Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
- Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
- Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024
- Integrate a Sampled FunctionONSITE INTERVIEW · Seen Nov 2024
Similar questions at other companies
- 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
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- Best Average Student ScoreGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Dictionary Word from Nine LettersGoogle · ONSITE INTERVIEWGoogle interview questions
- Two SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Keep the First Key for Each Distinct ValueOneTrust · ONSITE INTERVIEW
- Two Positions That Add Up to a TargetDocuSign · PHONE SCREEN · ONSITE INTERVIEWDocuSign interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)