Skip to content

Container With Most Water
Step by Step

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

Step through every pointer move in both directions, then share a link to the step where the area changed.

What You Watch While Lines Close In

The two lines, the water and the area record change on every step.

Width shrinks on every move, so only a taller line can add water. That is why the shorter line always moves.

CodeStory's visualiser shows both pointers, the container between them and the area record. Marked code lines follow every move, and you can step back, step forward or play.

Open Classic first. Then load Tall lines in the middle to see the move-the-taller bug miss the 9s.

How Two Pointers Find the Largest Area

Steps below follow the two lines on your screen.

  1. 1Place one pointer at both ends.
  2. 2Measure the water between the two lines.
  3. 3Move the pointer on the shorter line inward.
  4. 4Keep the largest area and stop when the pointers meet.

Container Mistakes the Visualiser Shows

A switch in the visualiser turns the mistake on, so you can 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. Load Classic to see it.

Container Inputs Worth Trying

Four presets cover ties, rising heights and tall lines in the middle.

  • ClassicSwitch on the move-the-taller bug here. It never lets go of the 1.
  • All the same heightEvery step is a tie, so either line can go.
  • Rising heightsr never moves because l is always the shorter line.
  • Tall lines in the middleSwitch on the move-the-taller bug here too. It never reaches the 9s.

Container Cost in Time and Space

Both pointers move inward once, so the check ends after about n steps.

Time
O(n)counted from the areas you measure
Space
O(1)two pointers and the largest area
Pattern
Two pointersMove the shorter line to find more water.

Practise Two Pointers Beyond Water Containers

Watching shows the pointers and solving trains you to choose which line moves.

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

Container Visualiser FAQs

Open the visualiser and follow the two lines inward.

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

CodeStory's visualiser shows both pointers, the container between them and the area record, beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of the container problem?

O(n) time and O(1) space. Live counts show about n steps instead of every pair of lines.

Why does my container with most water code fail on some inputs?

Moving the taller line is a common cause. Switch the bug on in the visualiser and watch the containers only shrink.

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 Two Pointers? Get a Free Callback

Tell CodeStory which two pointers step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from two pointers 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.