Data Structures & AlgorithmsHardGATE CS · DSA

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

  1. A. 40
  2. B. 42
  3. C. 8
  4. D. 32

Correct answer: B. 42

Checking all pairs (equivalently, the two-pointer sweep that moves the shorter boundary inward) finds the best area is 42, between indices 0 and 7. Only using the two outermost lines, 32, 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