Problem statement
Given an array of integers nums and an integer target, return the indices of the two different elements whose values add up to target.
Exactly one valid pair exists, and the same element cannot be used twice. Return the two indices in any order.
Examples
- Input
nums = [2, 7, 11, 15], target = 9
- Output
[0, 1]
Why: nums[0] + nums[1] = 2 + 7 = 9.
- Input
nums = [3, 2, 4], target = 6
- Output
[1, 2]
Why: 2 + 4 = 6. Index 0 cannot be paired with itself to make 3 + 3.
- Input
nums = [3, 3], target = 6
- Output
[0, 1]
Why: Equal values are still two different elements.
Constraints
- 2 ≤ nums.length ≤ 10⁴
- −10⁹ ≤ nums[i] ≤ 10⁹
- −10⁹ ≤ target ≤ 10⁹
- Exactly one valid answer exists.
How to answer it out loud
What it tests: Hash-map selection, correctness, handling duplicates, and the ability to improve a brute-force solution.
- 1Clarify
Clarify whether there is exactly one solution and whether the same element can be reused.
- 2Implement
Start with the pairwise baseline, then store previously seen values by index in a hash map.
- 3Verify
Test duplicates, negative values, and a solution at the ends; explain O(n) time and O(n) space.
From brute force to the best solution
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute force | Check every pair (i, j) with i < j. | O(n²) | O(1) |
| Sort + two pointers | Sort (value, index) pairs and move pointers inward from both ends. | O(n log n) | O(n) |
| One-pass hash mapBest | For each value, look up target − value among the values already seen. | O(n) | O(n) |
If the answer pair is (i, j) with i < j, then when the loop reaches j, the value at i is already in the map. Checking before inserting guarantees an element never pairs with itself.
Reference solution
Each version is self-contained and has been run against the examples and test cases on this page.
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} # value -> index of an earlier element for i, num in enumerate(nums): need = target - num if need in seen: # check BEFORE storing num, return [seen[need], i] # so an element never pairs with itself seen[num] = i return [] # unreachable when exactly one answer exists- Time
- O(n)
- Space
- O(n)
- Approach
- One-pass hash map
Edge cases to run before you say “done”
| Input | Expected | Why it matters |
|---|---|---|
| nums = [3, 3], target = 6 | [0, 1] | Duplicates: fails if you insert before you check. |
| nums = [-3, 4, 3, 90], target = 0 | [0, 2] | Negative numbers. |
| nums = [1, 5, 7, 9], target = 10 | [0, 3] | The pair sits at both ends. |
Answer the main question first, then take these on one at a time:
- What if the input is sorted?
- What if you need every valid pair?
- Can you solve it without extra space?
Check your recording or written solution against this list:
- Checking the current value after inserting it
- Returning values instead of indices without clarifying
- Claiming constant space for the hash map
This guide combines general interview-practice patterns with the public hiring material below. The practice prompt and coaching are PiriPiri AI editorial content, not official Amazon questions or answers.
Official sources reviewed 20 September 2026.