← Questions
Longest strictly increasing contiguous subarray
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Jan 2026. It tests arrays.
Given an integer array, return the length of the longest contiguous subarray whose elements strictly increase. Then solve a harder version: you may change at most one element to any integer you like before measuring.
Interface
Unless you agree on a different interface with the interviewer, assume:
longest_increasing_run(nums: list[int]) -> int- Part 2:
longest_increasing_run_one_change(nums: list[int]) -> int
Example
longest_increasing_run([1, 3, 5, 4, 7]) -> 3 # [1, 3, 5]
longest_increasing_run([2, 2, 2]) -> 1
longest_increasing_run_one_change([1, 3, 5, 4, 7]) -> 5 # change 4 to 6: [1, 3, 5, 6, 7]
longest_increasing_run_one_change([5, 6, 1, 2]) -> 3 # e.g. change 1 to 7: [5, 6, 7]
Constraints
-
0 <= len(nums) <= 10^5; values are integers. - One pass and O(1) extra space for Part 1.
Clarifying questions
- Strictly increasing, so equal neighbours break the run?
- What should an empty array return?
- In Part 2, can the new value be any integer, and is the change optional?
What a strong answer covers
- Part 1: one pass keeping the current run's length, reset to 1 when
nums[i] <= nums[i-1]; O(n) time, O(1) space. - Part 2: arrays
left[i](longest increasing run ending ati) andright[i](starting ati). Changingnums[i]joins the run ending ati-1with the run starting ati+1whennums[i+1] - nums[i-1] >= 2, since integers need room for a value strictly between; otherwise it extends one side by one. O(n) time. - Edge cases: changing the first or last element, arrays of length 1 or 2, equal values.
- A brute force check for small arrays.
Follow-ups
- Return the subarray itself, not only its length.
- Allow up to
kchanges. - The non-contiguous version: the longest increasing subsequence in O(n log n).
Where it's asked
- Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
