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.
- 1Walk the array from left to right.
- 2Ask the map whether the partner of the current number is there.
- 3Store the current number in the map so later numbers can find it.
- 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.
Practise Hash Maps Beyond Two Sum
Watching shows the lookups and solving trains you to reach for a map.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory checks your level, from hash map basics to interview ready.
- 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
- 3You decide. No pressure, no payment on the call.