Container With Most Water
Problem Statement
You are given n vertical lines. Line i stands at position i and has height height[i].
Pick two lines. Together with the x-axis they form a container. The water inside can only rise as high as the shorter of the two lines, and its width is the distance between them.
Return the most water a container can hold: the largest value of min(height[i], height[j]) × (j − i).
The lines in between do not block anything, and you may not tilt the container.
An integer array height of length n.
One integer: the maximum area over all pairs of lines.
Pick line 1 (height 8) and line 8 (height 7). The water rises to 7 and the width is 7, so the area is 7 × 7 = 49.
Only one pair: height 1, width 1.
- • 2 ≤ n ≤ 10^5
- • 0 ≤ height[i] ≤ 10^4
- • The largest area is at most 10^4 × 10^5 = 10^9, which fits in an int.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Measure the area of all n·(n−1)/2 pairs
Start at both ends; measure, then drop the shorter line
A container is just a pair of lines (i, j). Try every pair, compute min(height[i], height[j]) × (j − i), and keep the largest.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.
Circular Buffer & Ring Queue
Two pointer read/write head synchronization in production memory pools.
Pointers vs References Deep Dive
Pointer arithmetic, dereferencing semantics, and memory layout behind in-place array passes.
const Correctness & East-Const
Passing read-only spans with const int* without copying buffers.