Length 2:11Loading the video
Loading the video
Pick two walls from a row and find the pair that holds the most water. Start at both ends and always move the shorter wall inward, so one pass finds the best pair.
Narrated by an AI voice.
Here is a row of walls with different heights.
Pick two and fill the space between them with water.
Which pair holds the most?
Water spills over the shorter wall.
So the amount is the shorter height times the distance between the walls.
Trying every pair works, but nine walls already make thirty six pairs, and the count grows with the square of the number of walls.
Instead, start with one pointer on the first wall and one on the last.
This widest container is one unit high and eight wide, so it holds eight.
Moving the pointer at the taller wall can never help.
The width shrinks, and the water stays below the short wall.
So the shorter side steps inward.
Repeat until the pointers meet, keeping the best area.
The winning walls are eight and seven high, seven apart, so they hold forty nine units.
In JavaScript, that is one while loop.
Each pass works out the area, keeps the best, and steps the shorter side in.
On our row, it prints forty nine.
So nine walls take eight checks instead of thirty six.
Always move the shorter wall, and one sweep finds the biggest container.
Two pointers: how it works and when to use it
Two pointers walk a list from both ends, or one behind the other, so a pair search takes one pass. Signals, a worked example, code and practice.
Type any algorithm, interview problem or system, and get your own narrated video.
3 free videos with your own AI key. An AI key is a code from an AI company, such as Anthropic or OpenAI. Your own AI account pays for the writing. See pricing
Make a video
Length 2:11
Length 2:20
Length 1:56