Took me a second to realize the sea is on one side and you scan from that direction.
Clarify the problem by asking which direction the sea is (left or right) and whether buildings with equal height block the view. Then propose a linear scan from the sea side, keeping track of the maximum height seen so far, and collect buildings that exceed this maximum.
Pro tip: After presenting the solution, mention that the same logic can be applied from both ends if the sea is on both sides, and discuss how to handle duplicates or equal heights based on the clarified rules.
Ask the interviewer to confirm the direction of the sea (e.g., left or right) and whether a building blocks the view if its height is equal to the current maximum. Also confirm if the output should be in any specific order.
Decide to traverse from the sea side towards the land. For example, if the sea is on the left, start from the leftmost building and move right.
Initialize a variable to keep track of the maximum height encountered so far. For each building, compare its height to this maximum.
If the current building's height is greater than the maximum (or greater than or equal to, based on clarification), it has an unobstructed view. Add it to the result list and update the maximum height.
After traversing all buildings, return the list of heights. If the sea is on the right, either reverse the traversal or reverse the result to maintain the original order.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.