March 20
Two Sum
How to solve a classic interview problem while explaining the path, the trade-off, and the final choice.
Arrays and Hash Map Beginner 15 min TypeScript
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
- The question wants a fast decision for each number while you walk through the list.
- The core idea is not adding everything. It is identifying which complement is missing.
- If you remember what you have already seen, one pass is enough.
Common mistakes
- sorting too early and forgetting that the original indexes matter
- jumping to two nested loops without saying why the naive version costs O(n²)
- storing the current number before checking the complement and reusing the same index
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:
- compute the complement
- check whether it is already in the map
- return the stored index plus the current index if it exists
- 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
}
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:
- walk through the array once
- compute the complement
target - current - check whether that complement is already stored
- if yes, you found the answer
- if not, store the current number with its index
Step-by-step solution
- Create a
Hash MaplikeMap<number, number>to storenumber -> index. - For each position
index, readvalue. - Compute
complement = target - value. - If
seen.has(complement), return[seen.get(complement), index]. - 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 aHash Mapfor averageO(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.