Interview Prep — Classic Problem

Two Sum —
The interview every engineer has done at least once.

Two Sum is simple enough to solve in five minutes and rich enough to fill a 45-minute interview with follow-ups. This page focuses on the conversation around the problem — what to ask, what trips people up, and where interviewers push next. For the full step-by-step execution trace, see the dedicated visualization page linked below.

O(n)Optimal time
LC 1LeetCode problem #
PythonLanguage

Why does this specific problem get asked so often?

Because it cleanly tests three things at once: can you spot the brute-force O(n²) solution immediately, can you recognize the hash map optimization to O(n), and can you handle the implementation details correctly (like adding to the map after checking, not before). It's a compact test of pattern recognition.

Why visualization helps

Most candidates can describe the hash map approach in words but fumble the exact order of operations while actually typing it — especially the "check before insert" detail. LearnBug's full walkthrough shows the map building and the lookup firing in real time, which is where that detail actually clicks.

LearnBug — the hash map building and the lookup firing
🖼 Add a LearnBug screenshot here
What to Say First

Talk through all three approaches before coding

Brute force — O(n²)

State it first, out loud, even though you won't write it — it shows you can identify a working solution before optimizing, which is exactly the process interviewers want to see.

Say it, don't code it

Sort + two pointer — O(n log n)

Mention it as a valid alternative if the array is already sorted, or if the problem only needs values, not original indices — this shows you understand the trade-off it makes.

Only if sorted

Hash map — O(n) ✓ code this one

This is the one you actually implement. One pass, checking for the complement before inserting the current value — say the "before/after" detail out loud as you type it.

The answer

Follow-up questions interviewers actually ask

What should you ask the interviewer before writing any code?

Whether exactly one solution is guaranteed (the standard LeetCode version guarantees it), whether the array can contain duplicate values, and whether you should return indices or the values themselves. Asking these upfront signals you think about edge cases before diving in, which interviewers explicitly look for.

Why add to the hash map AFTER checking for the complement, not before?

If target=6 and the current value is 3, its complement is also 3. Adding 3 to the map before checking would let it match against itself, incorrectly returning the same index twice. Checking first guarantees any match found is a genuinely different, earlier element.

Follow-up: what if there are multiple valid pairs?

The standard problem guarantees exactly one answer, so this usually doesn't come up — but if it did, you'd collect every match into a results list instead of returning on the first one, which is a one-line change to the same hash map approach.

Follow-up: how does this extend to Three Sum?

Fix one element with an outer loop, then run a two-pointer (or hash map) Two Sum on the rest for each fixed element. With sorting + two pointers for the inner step, total time is O(n²) — this is one of the most common "what if we add another dimension" follow-ups.

Follow-up: can you solve it with O(1) extra space?

Only by sorting first (which itself costs O(n log n) time but no extra space beyond the sort) and using two pointers — but that approach loses the original indices unless you track them separately before sorting. If original indices are required, O(n) space via a hash map is essentially unavoidable.

What mistake do most candidates make live?

Forgetting the "check before insert" ordering, or accidentally checking target - i (the index) instead of target - nums[i] (the value) — a small but common typo under pressure. Narrating your logic out loud as you type tends to catch this before it becomes a bug.

Code Example

The solution you'd actually write

PythonHash map — O(n) time, O(n) space
def two_sum(nums, target):
    seen = {}    # value → index

    for i, num in enumerate(nums):
        complement = target - num

        if complement in seen:
            return [seen[complement], i]

        seen[num] = i   # add AFTER checking to avoid using the same element twice

    return []

Quick Reference

Brute ForceO(n²) / O(1)Say it, don't code it
Sort + Two PointerO(n log n) / O(1)Loses original indices
Hash MapO(n) / O(n)The answer to code
Common follow-upThree SumFix one element, Two Sum the rest

Watch the full step-by-step execution

Paste Two Sum into LearnBug and see the hash map build and the lookup fire, step by step.

Open Playground →