KatInterview

Longest Strictly Increasing Contiguous Run

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingarrayssliding-windowgreedy

Last updated

Tests whether a candidate can scan an array in a single linear pass to find the length of the longest strictly increasing contiguous run.

Problem

You are given an integer array. Your task is to find the length of the longest contiguous segment of elements where each value is strictly greater than the one before it.

A single element always forms a valid run of length 1. If the array is empty, return 0.

The comparison is strict: two adjacent equal values break the run, just as a decrease would.

Do not confuse this with the Longest Increasing Subsequence problem. Here the elements must be adjacent in the original array — no skipping allowed.

Constraints

  • 0 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The array may be empty, contain a single element, or consist entirely of equal or decreasing values.
  • Only the length of the longest run is required, not the run itself.

Examples

Example 1

Input: longest_strict_run([1, 2, 3, 2, 4, 5])

Output: 3

Example 2

Input: longest_strict_run([5, 4, 3, 2, 1])

Output: 1

Example 3

Input: longest_strict_run([1, 2, 2, 3, 4, 0, 1, 2, 3])

Output: 4

Hints

Hint 1

Try maintaining a counter for the run currently in progress. What resets it, and what extends it?

Hint 2

You only need to compare each element to the one immediately before it. One pass through the array is enough.

Hint 3

A common bug: forgetting to update your best-so-far on every step, not just when a run ends. A run that reaches the last element must still be counted.