1. Two Sum
easyAsked at AmazonGiven an array of integers and a target, return the indices of the two numbers that add up to the target. Amazon asks this to test whether you can articulate the space-for-time tradeoff before reaching for the optimal hash-map solution.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Amazon loops.
- Glassdoor (2026-Q1)— Amazon SDE I phone-screen reports cite this as the canonical warm-up.
- Reddit r/cscareerquestions (2025-11)— Recurring Amazon new-grad interview question.
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^9Only one valid answer exists.
Examples
Example 1
nums = [2,7,11,15], target = 9[0,1]Explanation: nums[0] + nums[1] == 9.
Example 2
nums = [3,2,4], target = 6[1,2]Example 3
nums = [3,3], target = 6[0,1]Approaches
1. Brute-force nested loops
Try every (i, j) pair with i < j.
- 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: Simplest but n^2. Mention before offering the hash map.
2. Hash map one-pass (optimal)
Scan once; for each num, check if target - num is in the map. Else store num -> index.
- 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: Trade O(n) space for O(n) time. One pass — by the time you'd want to pair X with Y, you've already inserted whichever appeared first.
Amazon-specific tips
Amazon interviewers specifically test whether you can articulate the brute-force first. State out loud: 'I could check every pair in O(n^2). But by spending O(n) space on a hash map, I can drop to O(n) time.' Jumping straight to the optimal is a flag that you've memorized rather than reasoned.
Common mistakes
- Returning the values instead of indices.
- Two-pass instead of one-pass (works but the one-pass is canonical).
- Forgetting that duplicates can appear (nums = [3,3] with target 6).
Follow-up questions
An interviewer at Amazon may pivot to one of these next:
- Two Sum II - Input Array Is Sorted (LC 167) — two-pointer.
- 3Sum (LC 15) — sort + fix + two-pointer.
- 4Sum (LC 18).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the one-pass approach correct?
When you arrive at index i, the map contains every prior index. If the complement was stored, it's earlier than i. So returning [stored_index, i] satisfies i < j.
Is the brute-force ever the right answer?
Only at extremely small input sizes where simplicity matters more than performance. For an Amazon interview, you should always offer the O(n) hash map.
Practice these live with InterviewChamp.AI
Drill Two Sum and other Amazon interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →