Find the First Matching Index in a Hidden Mountain Array

Quick Overview

Find the earliest target index through a hidden mountain-array interface while respecting a strict budget of 100 reads.

Find the First Matching Index in a Hidden Mountain Array

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

# Find the First Matching Index in a Hidden Mountain Array A hidden array increases strictly to one interior peak and then decreases strictly. Its length is at least three, and both sides of the peak are nonempty. Implement `Solution.findInMountainArray(target, mountainArr)`. Return the smallest index whose value equals `target`, or `-1` if the value is absent. A value may occur once on each side; in that case return the earlier index. ### Interface Access the array only through the supplied `MountainArray` object: - `get(k) -> int` returns the value at zero-based index `k`, for `0 <= k < length()`. - `length() -> int` returns the array length. You may make at most **100 calls to `get`** during one invocation. Calls to `length` do not consume that budget. Do not inspect stored values or the object's implementation. The arrays shown below are private setup data, not an extra candidate argument. ### Examples **Example 1:** The hidden array is `[2, 6, 11, 15, 11, 4]` and `target = 11`. Return `2`, even though index `4` also contains `11`. **Example 2:** The hidden array is `[1, 5, 9, 7, 3]` and `target = 6`. Return `-1`; the target lies between some stored values but is not present. ### Constraints - `3 <= mountainArr.length() <= 10_000`. - Every value and `target` is an integer in `0..1_000_000_000`. - For some `p` with `0 < p < n - 1`, values strictly increase through index `p` and strictly decrease afterward. - The hidden array stays unchanged throughout the call. - At most 100 `get` calls are permitted, including repeated reads of the same index. ### What a Strong Answer Covers - Return the earliest occurrence and distinguish absence from a valid endpoint index. - Keep all requested indices valid and justify the worst-case API-call count. - Handle the shortest array, the peak and targets appearing on only one side. ### Follow-up Questions - How should an independent test check both the result and the call limit? - Which guarantees would need reconsideration if the array allowed equal adjacent values?

Overview: Find the earliest target index through a hidden mountain-array interface while respecting a strict budget of 100 reads.

Microsoft logo
Microsoft
hardSoftware EngineerOtherCoding & Algorithms
0
0

Find the First Matching Index in a Hidden Mountain Array

A hidden array increases strictly to one interior peak and then decreases strictly. Its length is at least three, and both sides of the peak are nonempty.

Implement Solution.findInMountainArray(target, mountainArr). Return the smallest index whose value equals target, or -1 if the value is absent. A value may occur once on each side; in that case return the earlier index.

Interface

Access the array only through the supplied MountainArray object:

  • get(k) -> int returns the value at zero-based index k , for 0 <= k < length() .
  • length() -> int returns the array length.

You may make at most 100 calls to get during one invocation. Calls to length do not consume that budget. Do not inspect stored values or the object's implementation. The arrays shown below are private setup data, not an extra candidate argument.

Examples

Example 1: The hidden array is [2, 6, 11, 15, 11, 4] and target = 11. Return 2, even though index 4 also contains 11.

Example 2: The hidden array is [1, 5, 9, 7, 3] and target = 6. Return -1; the target lies between some stored values but is not present.

Constraints

  • 3 <= mountainArr.length() <= 10_000 .
  • Every value and target is an integer in 0..1_000_000_000 .
  • For some p with 0 < p < n - 1 , values strictly increase through index p and strictly decrease afterward.
  • The hidden array stays unchanged throughout the call.
  • At most 100 get calls are permitted, including repeated reads of the same index.

What a Strong Answer Covers Guidance

  • Return the earliest occurrence and distinguish absence from a valid endpoint index.
  • Keep all requested indices valid and justify the worst-case API-call count.
  • Handle the shortest array, the peak and targets appearing on only one side.

Follow-up Questions Guidance

  • How should an independent test check both the result and the call limit?
  • Which guarantees would need reconsideration if the array allowed equal adjacent values?