Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
Problem: Water is poured into the cells of a 1-dimensional array of heights [0,1,0,2,1,0,1,3,2,1,2,1]; each cell holds water up to min(maximum height to its left, maximum height to its right) minus its own height if positive. What is the total units of trapped water?
- A. 6
- B. 22
- C. 11
- D. 5
Correct answer: A. 6
With prefix and suffix maxima (or two pointers), each cell holds min(leftMax, rightMax) − height, totalling 6. Using max instead of min gives 22, using only the left maximum gives 11, and counting the cells that hold water gives 5.
More Coding Interview Problems questions
- Problem: Starting at 0 on a line, a walker takes steps +3, −2, +3, −2, … alternating, with 12 steps in total. How many distinct integer…
- Problem: Count the sequences of length 5 over {0, 1, 2} with no two adjacent equal digits and whose first digit is 0.
- Problem: The sorted array [1, 4, 4, 9, 12, 12, 15, 20] (0-indexed) may contain repeated values. Using a lower-bound search, which returns…
- Problem: How many subarrays of [1, 2, 2, 3] have max minus min at most 1? Run the code. python a = [1, 2, 2, 3] print(sum(1 for i in…
- Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the…
- Problem: What does this run-length encoder print for 'aaabccdd'? python s = 'aaabccdd' out = [] i = 0 while i < len(s): j = i while j <…