As reported: Oracle PMTS (Bangalore), round 5 (DSA, two questions), 2025 — given a list of towers, find the total visibility of each tower, looking left and right; described as similar to Container With Most Water / Trapping Rain Water, and run against test cases.
Towers stand in a row; heights[i] is the height of tower i. Looking left from tower i, it sees the towers next to it one by one until it reaches the first tower that is at least as tall as itself — that tower is seen too, and nothing behind it. If no tower on the left is that tall, it sees every tower on the left. Looking right works the same way.
The visibility of tower i is the number of towers it sees to the left plus the number it sees to the right. Return the visibility of every tower.
Example 1
heights = [3,1,2,4]
Output: [3,2,3,3]
Tower 0 (height 3) sees 1, 2 and then 4, which blocks the rest: 0 + 3. Tower 1 (height 1) sees 3 on the left and 2 on the right: 1 + 1. Tower 2 sees 1 and 3 on the left and 4 on the right: 2 + 1. Tower 3 sees all three on the left: 3 + 0.
Example 2
heights = [5,5,5]
Output: [1,2,1]
An equal tower blocks the view.
Constraints: 1 ≤ heights.length ≤ 10^5; 1 ≤ heights[i] ≤ 10^9.
Code runner format: one JSON value per line — heights. Implement visibility(heights); the harness prints the list.
Clarifications
- A blocking tower (at least as tall) is counted as seen; towers behind it are not.