This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
How can we efficiently find for each mountain the relevant mountains to compare itself against to check whether or not it would be visible?
Do you notice a pattern after sorting the peaks by their x-coordinates?
After sorting, process the peaks sequentially and use a monotonic stack to store currently visible mountains.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Sorting ensures mountains are processed in an order that reveals containment relationships. By ordering intervals by left boundary and handling ties carefully, larger mountains are considered before smaller overlapping ones. This makes it easy to detect when a mountain is fully covered.
Yes, this type of interval and monotonic stack problem is common in technical interviews. It tests understanding of sorting strategies, interval comparisons, and efficient scanning techniques. Companies often use similar patterns to evaluate algorithmic reasoning.
A monotonic stack or a simple running maximum is commonly used after sorting the intervals. These help track previously processed mountains and quickly determine whether the current one is hidden. The stack structure is particularly helpful when reasoning about overlapping intervals.
The optimal approach converts each mountain into an interval and sorts them by their left boundary while prioritizing larger right boundaries. By scanning the sorted intervals and tracking the maximum right boundary, you can detect which mountains are completely covered. This avoids brute-force comparisons and keeps the solution efficient.