← Back to All Algorithms

Medium Interview Frequency: ⭐⭐⭐⭐

Container With Most Water

Given height[i] as vertical lines on the x-axis, choose two lines that form a container with the maximum water area.

Two Pointers Greedy Reasoning

📚 Table of Contents

  1. Problem Statement
  2. Real-World Analogy
  3. Key Insights
  4. Solution Approaches
  5. Step-by-Step Walkthrough
  6. Complexity Analysis
  7. Edge Cases
  8. Test Cases
  9. Interview Tips
  10. Related Problems
  11. Key Takeaways

📋 Problem Statement

Two lines at indices i and j hold water with area: min(height[i], height[j]) * (j - i). Find the maximum possible area.

Examples

Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Best pair is indices 1 and 8: min(8,7) * (8-1) = 7 * 7 = 49.
Example 2:
Input: height = [1,1]
Output: 1
Only one container possible.

Constraints

🎯 Real-World Analogy

Sliding Fence Posts Across a Field

You are choosing two fence posts to stretch a tarp between them. The tarp height is limited by the shorter post, and width is the distance between posts.

If you keep the shorter post and move the taller one inward, width shrinks and limiting height does not improve. So that move cannot produce a better area.

The only potentially useful move is to drop the shorter boundary and hope to find a taller one, even though width decreases.

💡 Key Insights

💻 Solution Approaches

Approach 1: Brute Force

Time: O(N²) | Space: O(1)

Evaluate every pair (i, j), compute area, keep maximum. Correct but too slow for large inputs.

Approach 2: Two Pointers (Recommended)

Time: O(N) | Space: O(1)

Track left and right boundaries. Compute area, then move the pointer at the shorter line.

Python Implementation

from typing import List def max_area(height: List[int]) -> int: """ Two-pointer solution for maximum container area. Time: O(N) Space: O(1) """ left = 0 right = len(height) - 1 best = 0 while left < right: width = right - left h = min(height[left], height[right]) area = h * width if area > best: best = area # Move the limiting side. if height[left] < height[right]: left += 1 else: right -= 1 return best

JavaScript Implementation

function maxArea(height) { /** * Two-pointer solution for maximum container area. * * Time: O(N) * Space: O(1) */ let left = 0; let right = height.length - 1; let best = 0; while (left < right) { const width = right - left; const h = Math.min(height[left], height[right]); const area = h * width; if (area > best) { best = area; } // Move the limiting side. if (height[left] < height[right]) { left += 1; } else { right -= 1; } } return best; }

🔍 Step-by-Step Walkthrough

Trace height = [1,8,6,2,5,4,8,3,7]:

(0,8): min(1,7) * 8 = 8. Move left (shorter = 1).
(1,8): min(8,7) * 7 = 49. Best = 49. Move right (shorter = 7).
(1,7): min(8,3) * 6 = 18. Move right.
(1,6): min(8,8) * 5 = 40. Move either side; still below best.
Remaining widths shrink and no area beats 49. Answer = 49.

⚡ Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(1)

🔧 Edge Cases

🧪 Test Cases

Python

def test_max_area(): assert max_area([1, 8, 6, 2, 5, 4, 8, 3, 7]) == 49 assert max_area([1, 1]) == 1 assert max_area([4, 3, 2, 1, 4]) == 16 assert max_area([1, 2, 1]) == 2 assert max_area([0, 0, 0, 0]) == 0 print("✅ all tests passed")

JavaScript

function testMaxArea() { console.assert(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]) === 49, "test 1"); console.assert(maxArea([1, 1]) === 1, "test 2"); console.assert(maxArea([4, 3, 2, 1, 4]) === 16, "test 3"); console.assert(maxArea([1, 2, 1]) === 2, "test 4"); console.assert(maxArea([0, 0, 0, 0]) === 0, "test 5"); console.log("✅ all tests passed"); }

💼 Interview Tips

🎓 Key Takeaways

← Back to All Algorithms