Skip to content

Container with most water
one step at a time.

Start with the widest pair and always move the shorter line, the only move that can ever lead to more water.

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.

Start with the widest pair and always move the shorter line, the only move that can ever lead to more water. 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: both indexes walking, and the span they have already ruled out. 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. Container with most water on a clean example teaches you the shape; container with most water 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. 1Start one index at each end of the data.
  2. 2Compare what the two ends hold and decide which one can never improve.
  3. 3Move that pointer inward, which rules out everything it passed.
  4. 4Stop when the pointers meet: every possibility has been ruled in or out.

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 pairMeasures all n(n − 1)/2 containers instead of n − 1.
  • Bug: move the taller lineKeeps the short line and drops the tall one, so containers only shrink. Try "Classic".

Inputs worth trying.

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

  • ClassicRun the move-the-taller bug here: it never lets go of the 1.
  • All the same heightEvery step is a tie: either line can go.
  • Rising heightsr never moves: l is always the shorter line.
  • Tall lines in the middleRun the move-the-taller bug here too: it never reaches the 9s.

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(1)what the algorithm holds besides the input itself
Pattern
Two pointersTwo indexes walk towards each other, each step ruling something out.

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 container with most water.

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

Open the visualiser
What does the container with most water visualiser show?

Every step of the algorithm on input you choose: both indexes walking, and the span they have already ruled out, 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 container with most water?

O(n) time and O(1) 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 container with most water code fail on some inputs?

Usually one of the known traps. The visualiser ships the real ones for this algorithm (move the taller line), 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.