Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.maxArea(heights). For non-negative wall heights, return the maximum area formed by two walls and the baseline. At least two walls are provided.

Starter code

class Solution:
    def maxArea(self, heights: list[int]) -> int:
        pass
Test cases

mixed-heights

{
  "args": [
    [
      1,
      8,
      6,
      2,
      5,
      4,
      8,
      3,
      7
    ]
  ]
}

Expected: 49

Wizard outline
  1. Step 1: Initialize Solution.maxArea

    Replace the empty starter with the first real state owned by Solution.maxArea. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Two Walls case

    Complete the readable core algorithm for one representative Interview case. Use opposite-end pointers and prove why moving the shorter wall discards only non-improving pairs.

  4. Step 4: Harden the Mixed Heights boundary

    Repair the reviewed boundary and pass the complete submission contract. For the current shorter wall, every pair with a smaller width has height capped by that wall and cannot improve the area; discarding it therefore preserves every potentially better pair.

Footguns and prerequisites
  • Moving the taller wall cannot improve the limiting height while width always shrinks.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Discard Pairs with Two Pointers(opens in a new tab)

    Discard Pairs with Two Pointers isolates every pair outside [left, right] has already been counted or proven too large, and no unresolved pair is skipped. That focused state discipline is required when implementing container with most water as a complete Interview Problem.

Recommended approach and implementation

Start at both ends, record the current area, and move the pointer at the shorter wall inward.

Why it works: For the current shorter wall, every pair with a smaller width has height capped by that wall and cannot improve the area; discarding it therefore preserves every potentially better pair.

class Solution:
    def maxArea(self, heights):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        left = 0
        right = len(heights) - 1
        best = 0
        while left < right:
            best = max(best, (right - left) * min(heights[left], heights[right]))
            if heights[left] <= heights[right]:
                left += 1
            else:
                right -= 1
        return best
Similar exercises