Data Structures & AlgorithmsVery hardIBM · Deloitte pattern
Elevation map [3, 4, 1, 3, 4, 6, 3, 5, 4] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water is trapped in total?
- A. 10
- B. 33
- C. 6
- D. 4
Correct answer: C. 6
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 6 units. Using only the running left-max (ignoring the right side), as in 10, 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
- Heights [9, 2, 9, 6, 1, 6, 3] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal distance)…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 1, 0, 2, 2, 0, 1, 2, 2] (values 0, 1, 2 only)…
- What is the sum of the minimum element over every contiguous subarray of [3, 6, 5, 3, 8, 4, 9, 1]?
- Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 2, 2, 1, 0, 1, 1] (values 0, 1, 2 only) in…
- Array [85, 11, 19, 30, 40, 43, 70] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…