The basic idea clicked pretty fast, scan for peaks and expand left and right.
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.
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.
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.
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.
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.
State time complexity O(n) and space O(1) for one-pass. Discuss potential optimizations or trade-offs, and confirm no unnecessary passes.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.