Skip to content

Start typing to search patterns, problems and pages.

Two Pointers

Two people start at opposite ends of a hallway and walk toward each other. Each step rules out a whole row of options they will never need to check. Two pointers prune a search the same way: every move discards what cannot win.

⏱ about 15 minutesNeeds: arraysTime O(n)

2 · Watch

No sound? Captions are on by default.

3 · Now you drive

Keys:←→ stepSpace playR reset

Container With Most Water

or try
2–10 wall heights from 0 to 9, separated by spaces.
Start with the widest container: the first and last walls.Step 1 of 18. Starting out. 9 walls. Widest container first: wall 0 and wall 8.
Step 1 of 18
solution.ts▶ marks the line running now
function maxArea(height: number[]): number {
let left = 0, right = height.length - 1;
let best = 0;
while (left < right) {
const h = Math.min(height[left], height[right]);
best = Math.max(best, h * (right - left));
if (height[left] < height[right]) left++;
else right--;
}
return best;
}

4 · Your turn

0 of 3 problems solved
  1. Easy01

    Valid Palindrome

    Warm-up: ends walk inward, skipping what is not a letter.

  2. Medium02

    3Sum

    Sort first, then run two pointers from every start.

  3. Medium03

    Container With Most Water

    The one you just watched. Write it from memory.

UP NEXT · LESSON 3 OF 15Sliding WindowA range that grows and shrinks as it scans.