Skip to content

Two Sum With a Hash Map
Step by Step

Two sum asks a hash map whether the partner of every number, target minus x, has been seen already.

Step through every lookup in both directions, then share a link to the step where the pair appeared.

What You Watch While the Map Fills Up

The array, the map and the lookup change on every step.

Every number needs a partner that adds up to the target. Hash map lookups tell you in one step whether that partner already appeared.

CodeStory's visualiser shows the array and the map filling up beside it, key by key. Marked code lines follow every move, and you can step back, step forward or play.

Open Half the target first. Then load Two equal numbers to see why the lookup comes before the store.

How Two Sum Finds the Pair in One Pass

Steps below follow the map on your screen.

  1. 1Walk the array from left to right.
  2. 2Ask the map whether the partner of the current number is there.
  3. 3Store the current number in the map so later numbers can find it.
  4. 4Stop the moment a lookup succeeds, after one pass.

Two Sum Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can watch the state go wrong.

  • Brute force: every pairTwo nested loops try every pair, about n²/2 checks instead of n lookups.
  • Bug: store before lookingPuts nums[i] in the map before the lookup, so a number that is half the target pairs with itself. Load Half the target to see it.

Two Sum Inputs Worth Trying

Five presets cover half the target, duplicates and a missing pair.

  • Half the targetSwitch on the store-first bug here. 3 is half of 6, so it pairs with itself.
  • Two equal numbersBoth 5s are needed. Second 5 looks before it stores, so it finds the first.
  • No pairEvery number goes into the map and nothing ever matches.
  • Pair at the very endWorst case for brute force, so compare the check counts.
  • One number5 + 5 = 10, but only one 5 exists, so the store-first bug disagrees.

Two Sum Cost in Time and Space

One pass with one lookup per number keeps the time linear, while the map takes the extra space.

Time
O(n)counted from the lookups you step through
Space
O(n)the hash map that holds every number seen
Pattern
Arrays & hashingTrade memory for time by remembering what you have seen.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Two Sum Visualiser FAQs

Open the visualiser and follow the map key by key.

Open the visualiser
What does the two sum visualiser show?

CodeStory's visualiser shows the array and the hash map filling up key by key, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of two sum?

O(n) time and O(n) space with a hash map. Live counts show one lookup per number instead of every pair.

Why does my two sum code fail on some inputs?

Storing a number before the lookup is a common cause. Switch the bug on in the visualiser and watch a number pair with itself.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Hash Maps? Get a Free Callback

Tell CodeStory which hash map step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from hash map basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.