Data Structures & AlgorithmsHardIBM · Deloitte pattern
Elevation map [6, 1, 1, 0, 3, 3, 5, 5, 2, 0] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water is trapped in total?
- A. 34
- B. 17
- C. 26
- D. 9
Correct answer: B. 17
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 17 units. Using only the running left-max (ignoring the right side), as in 34, 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
- Array [75, 79, 80, 82, 6, 11, 23, 40, 63, 67] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary…
- Array [1, 2, 3, 4, 5, 6, 7, 8] is left-rotated by d = 7 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "BABAACBCCA" are anagrams of "AB"?
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(7), enqueue(2), dequeue(), enqueue(5), dequeue()…
- What is the postfix (Reverse Polish) form of the infix expression "f + d h -(i + e)", built with the standard stack-based conversion (^…