Find the Longest Strict Rise Followed by a Strict Fall

Quick Overview

Find the Longest Strict Rise Followed by a Strict Fall. A mountain subarray has at least three consecutive entries, rises strictly to one interior peak, then falls strictly.

Find the Longest Strict Rise Followed by a Strict Fall

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem A mountain subarray has at least three consecutive entries, rises strictly to one interior peak, then falls strictly. Return the length of the longest such subarray, or zero when none exists. Flat steps are excluded. ```cpp class Solution { public: int longestMountain(vector<int>& arr); }; ``` ### Examples **Example 1:** ```text arr = [1,3,5,4,2] Output: 5 Explanation: The whole array rises to5 then falls. ``` **Example 2:** ```text arr = [1,2,2,1] Output: 0 Explanation: The plateau prevents a strict peak. ``` ### Constraints - Array length lies in [1,10000]. - Entries are integers from 0 through 10000. ### Contract considerations Require both strict sides and contiguous entries. ```hint Read the boundary condition When does a new rising edge mean the old mountain has ended? ``` ### Boundary reminders Require both strict sides and contiguous entries. ### Optional extensions - Can the scan use only one pass? - Can it retain only constant extra storage?

Overview: Find the Longest Strict Rise Followed by a Strict Fall. A mountain subarray has at least three consecutive entries, rises strictly to one interior peak, then falls strictly.

TikTok logo
TikTok
Jun 27, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A mountain subarray has at least three consecutive entries, rises strictly to one interior peak, then falls strictly. Return the length of the longest such subarray, or zero when none exists. Flat steps are excluded.

class Solution {
public:
    int longestMountain(vector<int>& arr);
};

Examples

Example 1:

arr = [1,3,5,4,2]
Output: 5
Explanation: The whole array rises to5 then falls.

Example 2:

arr = [1,2,2,1]
Output: 0
Explanation: The plateau prevents a strict peak.

Constraints

  • Array length lies in [1,10000].
  • Entries are integers from 0 through 10000.

Contract considerations

Require both strict sides and contiguous entries.

Boundary reminders

Require both strict sides and contiguous entries.

Optional extensions

  • Can the scan use only one pass?
  • Can it retain only constant extra storage?