Amazon · Coding 1 of 5

Two Sum

Return the indices of the two numbers that add up to a target.

EasyArrayHash mapBest: O(n) time · O(n) space

Practice question: an editorial practice prompt, not a claim that Amazon asks this exact question. Interviews vary by role, level, team, and location.

Problem

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

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

Why: nums[0] + nums[1] = 2 + 7 = 9.

Example 2
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.

Example 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.
In the interview

How to answer it out loud

What it tests: Hash-map selection, correctness, handling duplicates, and the ability to improve a brute-force solution.

  1. 1
    Clarify

    Clarify whether there is exactly one solution and whether the same element can be reused.

  2. 2
    Implement

    Start with the pairwise baseline, then store previously seen values by index in a hash map.

  3. 3
    Verify

    Test duplicates, negative values, and a solution at the ends; explain O(n) time and O(n) space.

Approaches

From brute force to the best solution

ApproachIdeaTimeSpace
Brute forceCheck every pair (i, j) with i < j.O(n²)O(1)
Sort + two pointersSort (value, index) pairs and move pointers inward from both ends.O(n log n)O(n)
One-pass hash mapBestFor each value, look up target − value among the values already seen.O(n)O(n)
Key insight

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.

Solution

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
Test cases

Edge cases to run before you say “done”

InputExpectedWhy 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.
Likely follow-ups

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?
Avoid these mistakes

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
Sources and methodology

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.