← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Amazon SWE coding round, one problem the whole time: longest mountain in an array. Felt manageable but the edge cases are sneakier than they look.

Questions Asked (1)

Q1

Given an integer array, find the length of the longest contiguous mountain subarray. A mountain must have at least 3 elements, a strict ascending portion, a peak, and a strict descending portion. Return 0 if none exists.

Algorithms & Data Structures
Author's notes

The basic idea clicked pretty fast, scan for peaks and expand left and right.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a single pass to track the length of the current ascending and descending runs, updating the maximum mountain length when a peak is found. Alternatively, precompute increasing and decreasing run lengths from left and right, then combine them at each peak. Both approaches run in O(n) time and O(1) or O(n) space.

Pro tip: Clarify edge cases upfront (e.g., arrays with less than 3 elements, all increasing/decreasing, or plateaus) and mention that you'll handle them explicitly. This shows attention to detail and prevents bugs.

1. Understand the problem and edge cases

Confirm that a mountain requires at least 3 elements, strictly increasing then strictly decreasing, and that equal adjacent elements break the mountain. Ask about input constraints (size, values) and expected output for no mountain.

2. Choose an approach

Decide between a one-pass state machine (tracking up/down lengths) or precomputing left-to-right increasing and right-to-left decreasing arrays. Both are O(n) time; the one-pass uses O(1) space.

3. Implement the algorithm

For one-pass: iterate from index 1, maintain up and down counters. When ascending, increment up and reset down; when descending, increment down if up > 0; when equal, reset both. Update max when down > 0 and up > 0.

4. Test with examples

Walk through examples like [2,1,4,7,3,2,5] (longest mountain length 5) and edge cases like [2,2,2] (0) and [1,2,3] (0). Verify the algorithm handles peaks at boundaries.

5. Analyze complexity and optimize

State time complexity O(n) and space O(1) for one-pass. Discuss potential optimizations or trade-offs, and confirm no unnecessary passes.

Key Points to Mention

  • Definition of a mountain: at least 3 elements, strictly increasing then strictly decreasing.
  • Edge cases: arrays with fewer than 3 elements, all increasing, all decreasing, or containing plateaus.
  • One-pass approach with state variables (up, down) and updating max when a peak is found.
  • Precomputation approach: compute increasing run lengths from left and decreasing from right, then combine at each index.
  • Time and space complexity: O(n) time, O(1) space for one-pass; O(n) space for precomputation.
  • Handling equal adjacent elements: they break the mountain, so reset counters.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.