Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard

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

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

Correct answer: C. 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, 8, 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