Data Structures & AlgorithmsHardGATE CS · DSA
Elevation map [4, 6, 0, 2, 6, 6, 6, 2] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water is trapped in total?
- A. 32
- B. 14
- C. 10
- D. 6
Correct answer: C. 10
Water trapped above position i equals min(max to its left, max to its right) − height[i], summed over all positions (never negative). Computing this gives 10 units. Using only the running left-max (ignoring the right side), as in 14, overcounts positions where the right boundary is actually the limiting (shorter) wall.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 2, 0, 0, 2, 1, 0, 2, 0] (values 0, 1, 2 only)…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [3, 4, 4, 4, 4, 1, 3, 4, 4]. What candidate and…
- A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(9)?
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 2, 2, 2, 1, 1, 0] (values 0, 1, 2 only) in…