Prepin
Log in

Given an array of numbers representing the heights of buildings from a beach, return an array that includes the heights of the buildings that have a view of the beach.

Approach / Explanation

The interview question requires you to address a problem using an algorithm. You will be given an array of numbers, each representing the height of adjacent buildings, with the sea beach located beyond the last building in the array. In a real-life scenario, only those buildings can see the beach that are either taller than the buildings between them and the beach or have no intervening buildings. More specifically, a building can see the sea if it is not obstructed by any taller building to its right. Therefore, your task is to identify the heights of the buildings that can view the beach. Here are some guiding points to help you approach and solve this problem: Traverse the array from right to left (since the sea lies beyond the last building). Maintain a record of the maximum height encountered thus far, starting with a value of 0, as all positive numbers exceed 0. If the height of the current building surpasses the maximum height recorded so far, include it in your result set and update the maximum height accordingly.

Suggested Answer

For instance, consider an input array of [4, 2, 3, 1]. We begin with the last building. The highest height we've seen so far is 0. The height of the last building is 1, which exceeds 0. Therefore, it can see the sea. The first element in our result array is 1, and the maximum height is now 1. The next building has a height of 3, which can also see the sea since it is greater than the current maximum height of 1. We then add 3 to the result array and update the maximum height to 3. The following building has a height of 2, but it cannot see the sea because it is less than the maximum height of 3. Finally, the first building has a height of 4, which can see the sea as it surpasses the maximum height of 3. Thus, we add 4 to the result array, making the maximum height now 4. Consequently, the final output array is [1, 3, 4].

Alternative Answer

Let's examine another example where the input array is [1, 4, 3, 2]. Starting from the last building, the maximum height observed so far is 0. The height of the last building is 2, which exceeds 0. This building can see the sea, so we add 2 to the result array and update the maximum height to 2. The next building has a height of 3, which is greater than the current maximum height of 2. Therefore, we add 3 to the result array and update the maximum height to 3. The following building cannot see the sea, as its height of 4 is merely greater than the maximum height. Nonetheless, we add 4 to the result array and update the maximum height to 4. The first building has a height of 1, which is less than the maximum height of 4, meaning it cannot see the sea. Consequently, the output array is [2, 3, 4].

Question Details

Difficulty & Category

Medium
algorithms

Companies

Meta (Facebook)

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.