How do you find the container with the most water in an array?

DSA
Medium
79.2K views

Given n vertical lines, this problem asks to find two lines that together with the x-axis form a container holding the most water. It tests the two-pointer technique.

Why Interviewers Ask This

This question evaluates a candidate's ability to apply the two-pointer technique to geometric problems. Interviewers look for the insight that moving the shorter line inward is the only viable strategy to potentially increase area. It tests logical deduction and mathematical reasoning regarding area constraints.

How to Answer This Question

Explain the formula for area: min(height[left], height[right]) * (right - left). Start with pointers at both ends. Explain that moving the taller pointer inward cannot increase the area since the width decreases and height is limited by the shorter one. Therefore, always move the shorter pointer. Iterate until pointers meet. Analyze time complexity.

Key Points to Cover

  • Two-pointer initialization
  • Moving the shorter line logic
  • Area calculation formula
  • O(n) efficiency

Sample Answer

I would use the two-pointer approach starting with the widest possible container. The area is determined by the shorter line and the distance between them. Since moving the taller line inward only reduces width without i…

Common Mistakes to Avoid

  • Moving the taller pointer incorrectly
  • Calculating area using the wrong height
  • Missing the termination condition

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 89 DSA questions