As reported: Confluent SSE 2 online assessment on HackerRank (2026), 60 minutes: "Visible Towers",
countVisibleTowers.
There are n towers in a row; tower i has height height[i]. For every tower, count how many other towers it can see, looking both left and right.
Tower i can see tower j (j ≠ i) if every tower strictly between them is shorter than tower j. Adjacent towers always see each other.
Return an array whose i-th value is the number of towers visible from tower i.
Example 1
height = [5, 2, 10, 1]
Output: [2, 2, 3, 1]
- Tower 0 (5) sees tower 1 and tower 2 (the 2 between them is shorter than 10); it cannot see tower 3 (10 is not shorter than 1).
- Tower 1 (2) sees towers 0 and 2.
- Tower 2 (10) sees all three others.
- Tower 3 (1) sees only tower 2.
Example 2
height = [3, 6, 2, 4, 5]
Output: [1, 4, 3, 3, 2]
Constraints: 1 ≤ n ≤ 10^5, 1 ≤ height[i] ≤ 10^9.
Code runner format: one JSON value per line: height. Implement countVisibleTowers(height).
Clarifications
- The height of the looking tower does not matter; only the towers between the two and the height of the tower being looked at.
- "Shorter" is strict: a tower between them of equal height blocks the view.