Magicsheet logo

Find Indices of Stable Mountains

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Topics

Find Indices of Stable Mountains

What is this problem about?

The Find Indices of Stable Mountains coding problem asks you to identify "stable" mountains based on a height threshold and their predecessor's height. You are given an array of heights and a threshold. A mountain at index ii is considered stable if the mountain immediately before it (at index i1i-1) has a height strictly greater than the threshold. You need to return all valid indices i>0i > 0.

Why is this asked in interviews?

Google uses this "Easy" question to test basic array indexing and attention to problem descriptions. It is a test of "Relative State"—making a decision about an element based on its neighbor. It checks if you can handle 0-based indexing correctly and whether you can avoid common off-by-one errors when looking at i1i-1.

Algorithmic pattern used

This problem uses a Linear Scan.

  1. Iterate through the array starting from the second element (index 1).
  2. For each index ii, check the value at i1i-1.
  3. If height[i-1] > threshold, add ii to your result list.
  4. Since you are scanning from left to right, the resulting indices will naturally be in sorted order.

Example explanation

heights = [1, 2, 1, 3, 5], threshold = 2

  1. Check index 1: Predecessor (index 0) is 1. 121 \le 2. Not stable.
  2. Check index 2: Predecessor (index 1) is 2. 222 \le 2. Not stable.
  3. Check index 3: Predecessor (index 2) is 1. 121 \le 2. Not stable.
  4. Check index 4: Predecessor (index 3) is 3. 3>23 > 2. Stable! Result: [4].

Common mistakes candidates make

  • Checking the current mountain: Comparing height[i] to the threshold instead of height[i-1].
  • Starting at index 0: Trying to check a predecessor for the first mountain, which results in an IndexOutOfBounds error.
  • Inclusive threshold: Using \ge instead of >> when the problem specifies "strictly greater than."

Interview preparation tip

Simple problems are about precision. Read the definition of "stable" carefully. If it depends on the previous element, your loop must start at 1. If it depended on the next element, your loop would end at n2n-2.

Similar Questions