Two Sum
Find two numbers that add up to a target. The single most-asked interview question.
What is Two Sum?
Given an array and a target, return the indices of the two numbers that add up to the target. There is exactly one solution.
Where you will see it
Real systems that use this exact idea. Tap a card to open it.
Common traps
The mistakes interviewers watch for. Guess the fix, then reveal it.
FixCheck the map BEFORE storing the current number. Store-first lets a single element pair with itself (e.g. [3], target 6 returns [0, 0]).
FixRe-read the spec: classic Two Sum asks for indices. Returning values silently fails half the hidden tests.
FixSorting destroys the original indices. Only sort if the problem asks for values or gives you a sorted array.
Interview prep
Questions you could be asked, with the depth an interviewer wants to hear.
The hash-map solution stores each number's index as you walk the array, and for each number checks whether target - num was already seen. That is O(n) time and O(n) space. The brute force is O(n^2) time but O(1) space.
O(n) time · O(n) space
Real worldThis is the single most-asked interview question - a warm-up that tests whether you reach for a hash map.
DeeperMention both solutions and when each wins: map when speed matters, brute force when memory is tight.