Skip to content

Two sum
one step at a time.

For each number, ask a hash map whether its partner (target − x) has already been seen.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

For each number, ask a hash map whether its partner (target − x) has already been seen. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the array, and the map filling up beside it, key by key. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. Two sum on a clean example teaches you the shape; two sum on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Walk the array left to right, one element at a time.
  2. 2At each element, ask the map what it has already seen.
  3. 3Write the element into the map so later elements can find it.
  4. 4The answer appears the moment a lookup succeeds, in one pass.

Where people get this one wrong.

These are not made up. Each one is a switch in the tool: turn it on, run the same input, and 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. Try "Half the target".

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

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

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(n)counted as real operations while it runs, not quoted from a table
Space
O(n)what the algorithm holds besides the input itself
Pattern
Arrays & hashingTrade memory for time: remember what you have seen.

Once you can see it, go and use it.

Watching is the first ten minutes. The pattern sticks when you solve with it.

Everything in the app, in one place.

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

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About two sum.

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the two sum visualiser show?

Every step of the algorithm on input you choose: the array, and the map filling up beside it, key by key, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of two sum?

O(n) time and O(n) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Why does my two sum code fail on some inputs?

Usually one of the known traps. The visualiser ships the real ones for this algorithm (store before looking), so you can switch the bug on, run it, and watch exactly where the state goes wrong.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.