Data Structures & AlgorithmsVery hardProduct-company MCQ pattern

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

  1. A. 27
  2. B. 42
  3. C. 40
  4. D. 7

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 1 and 8. Only using the two outermost lines, 27, 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