Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
Elevation map [2, 2, 5, 0, 2, 0, 3, 1, 1, 1] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water is trapped in total?
- A. 4
- B. 27
- C. 7
- D. 17
Correct answer: C. 7
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 7 units. Using only the running left-max (ignoring the right side), as in 27, 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 of fixed size with a character-frequency count, how many substrings of "BAAAABBAACB" are anagrams of "AB"?
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- Floyd's cycle detection runs on a linked list 1 → 2 → ... → 8 whose last node's next pointer points back to node 4 (0-based position 3)…
- Array [36, 53, 71, 81, 12, 18, 20, 26] is a sorted array rotated at pivot 4. Using the standard O(log n) rotated-array binary search…
- Binary search for first/last occurrence runs on the sorted array [2, 7, 9, 11, 16, 16, 17, 18] for target 16. What (first index, last…