KatInterview

Longest Strictly Rising Streak in a Row of Readings

Arrays & HashingAuto-graded · 23 test cases
EasyAuto-gradedArrays & Hashingarrayssliding-windowtwo-pointers

Last updated

Scan an integer list once to find the longest block of adjacent, strictly rising values, handling empty input, equal neighbors, and runs that reach the end.

Longest Rising Streak

You are given a list of integers readings. A streak is a block of consecutive positions in the list in which every value is strictly larger than the value just before it.

More formally, the positions i, i+1, ..., j form a streak if readings[k] < readings[k+1] for every k with i <= k < j. A block containing a single element is always a streak of length 1.

Return the length of the longest streak in readings.

Rules

  • The elements must be adjacent in the list. Skipping positions is not allowed.
  • Two equal neighbors do not count as rising, so they end the current streak.
  • If readings is empty, return 0.
  • If readings is non-empty, the answer is at least 1.

Constraints

  • 0 <= len(readings) <= 10^5
  • -10^9 <= readings[i] <= 10^9
  • Aim for O(n) time and O(1) extra space.

Examples

Example 1

Input: longest_rising_streak([1, 3, 5, 4, 7])

Output: 3

Example 2

Input: longest_rising_streak([2, 2, 2, 2, 2])

Output: 1

Example 3

Input: longest_rising_streak([])

Output: 0

Hints

Hint 1

Because the elements must be adjacent, each element either extends the streak that ended just before it or begins a brand new one.

Hint 2

Keep two numbers while scanning left to right: the length of the streak ending at the current position, and the best length seen so far.

Hint 3

Compare using strict greater-than so equal neighbors reset the streak, and return 0 right away for an empty list.