Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard

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

  1. A. 8
  2. B. 56
  3. C. 9
  4. D. 54

Correct answer: B. 56

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