How do you arrange buildings to ensure a clear view of the sea?

Coding
Medium
Microsoft
84K views

This problem involves finding visible elements in an array based on height constraints, testing stack-based solutions.

Why Interviewers Ask This

It tests your ability to optimize for time complexity while maintaining logical correctness. The problem is a variation of the 'next greater element' pattern, common in technical interviews.

How to Answer This Question

Iterate through the array from right to left. Maintain a variable for the maximum height seen so far. If a building is taller than the max, it has a view. Update the max height. This ensures O(n) time complexity.

Key Points to Cover

  • Right-to-left iteration
  • Tracking maximum height
  • O(n) time complexity
  • View definition logic

Sample Answer

I iterate through the building heights from right to left, assuming the sea is on the right. I keep track of the maximum height encountered so far. If the current building's height is greater than this maximum, it has a…

Common Mistakes to Avoid

  • Using nested loops (O(n^2))
  • Incorrect direction of iteration
  • Failing to update max height

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 80 Coding questionsBrowse all 107 Microsoft questions