Array•Pattern: Two Pointers / Greedy•Medium

Container with Most Water

Problem Intuition & Objective

Given heights of vertical lines, find two that together with the x-axis form a container holding the most water.

Time ComplexityO(n)
Optimal
Space ComplexityO(1)
Memory Bounds
Optimal Algorithmic Approach

Greedy Two-Pointer: the width decreases with each move, so always sacrifice the smaller height to potentially gain more from height.

Core Invariants & Edge Cases

  • Start with widest possible container (left=0, right=n-1)
  • Always move the pointer with the shorter height inward
  • Area = min(h[left], h[right]) × (right - left)
Previous Problem3Sum — find all unique triplets that sum to zeroNext Problem Find all duplicates in an array (values in [1, n])

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.