Data Structures & AlgorithmsHardFlipkart/Uber OA · placement

Bars of width 1 have heights [7, 3, 3, 6, 1, 3, 7, 1, 1]. How many units of rain water are trapped between them (two-pointer method with left-max and right-max)?

  1. A. 19
  2. B. 42
  3. C. 20
  4. D. 18

Correct answer: A. 19

Water above bar i is min(max to its left, max to its right) − h[i], never negative. Summing gives 19; filling every bar to the global maximum overestimates it.

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

Exams that ask this topic