Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Find the maximum amount of water held between two vertical lines of heights [1, 8, 6, 2, 5, 4, 8, 3, 7] (the water is limited by the shorter line, and the width is the index difference).

  1. A. 49
  2. B. 56
  3. C. 8
  4. D. 64

Correct answer: A. 49

Two pointers move the shorter line inward; the best pair is heights 8 and 7 at indices 1 and 8: min(8, 7) × 7 = 49. Using the taller line (or width j − i + 1) gives 56, the two outer lines give 1 × 8 = 8, and 64 multiplies the tallest height by the full width.

More Coding Interview Problems questions

Exams that ask this topic