Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern

Heights [9, 2, 9, 6, 1, 6, 3] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal distance) obtainable between any two lines?

  1. A. 28
  2. B. 18
  3. C. 6
  4. D. 30

Correct answer: D. 30

Checking all pairs (equivalently, the two-pointer sweep that moves the shorter boundary inward) finds the best area is 30, between indices 0 and 5. Only using the two outermost lines, 18, is not always optimal since a taller inner pair can win.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic