Container With Most Water

Python data engineering interview problem. Difficulty: beginner. Pattern: Two Pointers. About 10 minutes. Free to practice.

Max area formed by two vertical lines and the x-axis. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Imagine a container made by choosing two vertical walls from a row of walls. Its width is the distance between the walls, and its water height is limited by the shorter wall. Choose two different positions and return the largest possible area. The area is (right - left) * min(height[left], height[right]), so a tall wall cannot make up for a short wall. Write the function max_area(height: list) -> int. In [1, 8, 6, 2, 5, 4, 8, 3, 7], the walls at positions 1 and 8 hold area 7 * 7 = 49. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.

Requirements

  • Consider the distance between the two selected positions.
  • Use the shorter wall as the height of the container.
  • Return one integer, the largest area found.

Constraints

  • height is a list of non-negative wall heights.
  • The two positions must be different, and the return type is int.

Examples

Input: max_area([1, 8, 6, 2, 5, 4, 8, 3, 7]) Output: 49 Lines at index 1 and 8.

Topics: lakebench, python, area, two pointers.

More Python interview questions · All interview problems · Learn data engineering

beginner

Container With Most Water

Interview-style drill: Max area formed by two vertical lines and the x-axis.

Implement `max_area(height: list) -> int`. Example: `[1, 8, 6, 2, 5, 4, 8, 3, 7]` → `49`. Keep the harness.