Skip to main content

Two Sum

How to solve a classic interview problem while explaining the path, the trade-off, and the final choice.

Given an array of integers nums and an integer target, return the indexes of two numbers whose sum equals target.

You can assume there is exactly one valid answer, and you cannot use the same element twice.

Example:

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

nums[0] + nums[1] = 2 + 7 = 9

What to notice before coding

Common mistakes

Step 1 — Start with the honest baseline

The first correct version checks every pair.

It proves you understood the problem, but it costs O(n²).

Step 2 — Ask what information is missing

While you read the current number, you do not need every past calculation.

You only need to know whether the complement target - value has already appeared.

Step 3 — Match the question to the structure

That is why a Hash Map fits:

  • key: number already seen
  • value: index where it appeared

Step 4 — Get the order right

For each number:

  1. compute the complement
  2. check whether it is already in the map
  3. return the stored index plus the current index if it exists
  4. otherwise store the current number

Checking first and storing after prevents reusing the same element twice.

The interactive editor needs JavaScript. You can still read the prompt and copy the starter code below.

Starter code

export function twoSum(nums: number[], target: number): [number, number] | null {
  // your solution here
  return null
}
solution.ts
Stuck? Hints available.

Haven't solved it yet?

Viewing the solution now may reduce learning. It's worth trying a bit more.

Open the reference solution

Without JavaScript, the reference solution is shown inline instead of in a dialog.

Solution

Final complexity

Time: O(n)

Space: O(n)

How to think before you code

The first correct version compares each number against all later numbers.

It proves understanding, but it costs O(n²).

The next question is:

While I am looking at the current number, what do I wish I knew immediately?

The answer is: I want to know whether its complement has already appeared.

If the target is 9 and the current number is 7, I need to know whether I have already seen 2.

That turns the problem into:

  1. walk through the array once
  2. compute the complement target - current
  3. check whether that complement is already stored
  4. if yes, you found the answer
  5. if not, store the current number with its index

Step-by-step solution

  1. Create a Hash Map like Map<number, number> to store number -> index.
  2. For each position index, read value.
  3. Compute complement = target - value.
  4. If seen.has(complement), return [seen.get(complement), index].
  5. Otherwise store seen.set(value, index).

The important detail is the order:

  • first check whether the complement already exists
  • then store the current value

That prevents reusing the same element.

TypeScript solution

export function twoSum(nums: number[], target: number): [number, number] | null {
  const seen = new Map<number, number>()

  for (let index = 0; index < nums.length; index += 1) {
    const value = nums[index]
    const complement = target - value

    if (seen.has(complement)) {
      return [seen.get(complement)!, index]
    }

    seen.set(value, index)
  }

  return null
}

What to say in the interview

A good short explanation would be:

The simplest version compares every pair and costs O(n²). Since I only need to know whether the complement has already appeared, I can use a Hash Map for average O(1) lookup and solve it in one pass.

That shows three strong signals:

  • you understood the naive version
  • you justified the optimization
  • you connected the data structure to the actual need of the problem

When this pattern shows up again

The same idea comes back whenever the problem asks you to:

  • remember what has already appeared
  • detect repetition
  • answer whether a complement exists
  • group or query quickly during iteration

That is why this challenge is not only about Two Sum.

It is about recognizing when fast-access memory simplifies the decision.

The point is not memorizing Hash Map.

The real signal is noticing that the problem asks for memory during the pass.

Next challenge Contains Duplicate

Related challenges

Related articles