Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT

Vertical lines have heights [1, 8, 6, 2, 5, 4, 8, 3, 7, 9, 1, 6] at positions 0 to 11. Choose two lines; the water they hold is min(height) × distance. What is the maximum water that can be held?

  1. A. 59
  2. B. 11
  3. C. 49
  4. D. 64

Correct answer: D. 64

Two pointers from both ends: the shorter wall limits the water, so moving the taller pointer can never help; move the shorter one. Taking the tallest line with the farthest partner is not optimal.

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

Exams that ask this topic