10. Container With Most Water

10. Container With Most Water

Medium
48.0%
1.5k
arraygreedytwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A cloud infrastructure team monitors the capacity of a data pipeline. The pipeline can be modeled as a set of vertical barriers along a line, where each barrier has a height representing the maximum safe throughput at that point.

To plan for peak traffic, the team wants to choose two barriers that form a container able to hold the maximum amount of capacity. The capacity is limited by:

  • the shorter of the two barrier heights
  • the distance between the two barriers

The barrier heights are stored in an integer array height, where height[i] is the height of the barrier at position i.

This is a common coding interview and DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given an integer array height, return the maximum amount of capacity (water) a container can store by selecting two different indices i and j.

The container area is defined as:

  • area = min(height[i], height[j]) * (j - i)

You may not tilt the container.

Input Format

  • height: an array of integers representing barrier heights

Output Format

  • A single integer representing the maximum possible area

Examples

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: min(8, 7) * (8 - 1) = 7 * 7 = 49

Example 2:

Input: height = [1,1]
Output: 1

Constraints

  • 2 <= height.length <= 100,000
  • 0 <= height[i] <= 10,000

Loading editor...

[1,8,6,2,5,4,8,3,7]
49

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback