Data Structures & AlgorithmsHardGATE CS · TCS NQT

How much rain water is trapped between bars of heights [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]?

  1. A. 11
  2. B. 6
  3. C. 3
  4. D. 22

Correct answer: B. 6

Water above each bar is min(max on left, max on right) − height, giving 6; the two-pointer scan does this without both arrays. Using only the left maximum gives 11, comparing only the immediate neighbours gives 3, and 22 fills everything up to the tallest bar.

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

Exams that ask this topic