Skip to main content

1. Two Sum

easyAsked at JPMorgan

Two Sum is the canonical JPMorgan SDE phone-screen warm-up: given an integer array and a target, return the indices of the two numbers that add up to the target. JPMorgan interviewers grade your willingness to narrate the brute-force-to-optimal tradeoff before writing the hash-map version.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in JPMorgan loops.

  • Glassdoor (2026-Q1)Recurring JPMorgan SDE phone-screen warm-up across new-grad and lateral candidate reviews.
  • LeetCode (2026-Q1)Tagged JPMorgan on the LeetCode company tag page (premium).
  • Reddit r/cscareerquestions (2025-11)Multiple JPMorgan Software Engineer Programme reports list Two Sum as the first 15-minute warm-up.

Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists.

Examples

Example 1

Input
nums = [2,7,11,15], target = 9
Output
[0,1]

Explanation: nums[0] + nums[1] == 9, so we return [0, 1].

Example 2

Input
nums = [3,2,4], target = 6
Output
[1,2]

Example 3

Input
nums = [3,3], target = 6
Output
[0,1]

Approaches

1. Brute-force nested loops

Try every (i, j) pair with i < j and return when nums[i] + nums[j] == target.

Time
O(n^2)
Space
O(1)
function twoSumBrute(nums, target) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) return [i, j];
    }
  }
  return [];
}

Tradeoff: Trivial to reason about and zero extra space, but JPMorgan interviewers explicitly look for the candidate to name the cost and pivot to a hash-based approach. Submitting only this version on a phone screen risks a 'didn't reach for the optimal' note.

2. Hash map (optimal, one-pass)

Store each value's index in a hash map as you scan. For each new num, look up target - num in the map.

Time
O(n)
Space
O(n)
function twoSum(nums, target) {
  const seen = new Map();
  for (let i = 0; i < nums.length; i++) {
    const need = target - nums[i];
    if (seen.has(need)) return [seen.get(need), i];
    seen.set(nums[i], i);
  }
  return [];
}

Tradeoff: O(n) time at O(n) extra space — the canonical space-for-time trade. One pass works because by the time you would want to pair X with Y, you have already inserted whichever appeared first.

JPMorgan-specific tips

JPMorgan SDE interviewers ask this on the phone screen for the Software Engineer Programme and lateral SWE roles. The grading rubric explicitly looks for two things: (1) you name the O(n^2) baseline before writing code, and (2) you correctly identify the hash-map trade as O(n) extra space for O(n) time. The interviewer often follows up with 'what changes if the array is already sorted?' to see whether you spot the two-pointer O(1)-space variant.

Common mistakes

  • Returning the values instead of the indices.
  • Building the hash map in a first pass then scanning in a second pass when a one-pass solution is cleaner.
  • Forgetting that duplicate values are allowed: nums = [3,3] with target 6 must return [0,1].
  • Using nums.indexOf(target - nums[i]) inside the loop, which silently makes the solution O(n^2).

Follow-up questions

An interviewer at JPMorgan may pivot to one of these next:

  • What if the array were sorted? (Two-pointer in O(1) extra space.)
  • What if we needed all pairs that sum to target, not just one? (Frequency map + duplicate handling.)
  • Extend to Three Sum (LC 15) — sort + fix one + two-pointer the remainder, O(n^2).
  • What if the array does not fit in memory? (External hash + chunked passes.)

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Does JPMorgan still ask Two Sum in 2026?

Yes. Public Glassdoor and LeetCode tag reports through Q1 2026 list it as the most common Software Engineer Programme phone-screen warm-up. Expect it as the first 10 to 15 minutes, then a harder follow-up (Three Sum or a sorted-array variant).

Is the one-pass hash map actually required?

Both one-pass and two-pass pass the correctness rubric, but the one-pass demonstrates the deeper invariant: when you reach index i, the map already contains every prior index, so any valid pair whose later element is i must have its earlier element stored. JPMorgan grade sheets we have seen prefer that articulation.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Two Sum and other JPMorgan interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →