Find Longest Increasing Continuous Subarray
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Given an integer array `nums`, return the length of the longest contiguous strictly increasing subarray.
"Contiguous" means the elements must appear next to each other in the original array. The subarray must be strictly increasing, so each next element must be greater than the previous element.
Example:
- Input: `[1, 3, 5, 4, 7]`
- Output: `3`
- Explanation: `[1, 3, 5]` is the longest contiguous strictly increasing segment.
### Constraints & Assumptions
- Aim for `O(n)` time and `O(1)` extra space.
- If the array is empty, return `0`.
- If the array has one element, return `1`.
- Equal adjacent values break a strictly increasing run.
- You only need to return the length unless asked for the subarray itself.
### Clarifying Questions to Ask
- Should an empty array return `0` or be treated as invalid input?
- Do we need the actual subarray or only its length?
- Are duplicate values allowed, and should equal values break the run?
### What a Strong Answer Covers
- A one-pass scan that tracks the current increasing run length.
- Resetting the current run when `nums[i] <= nums[i - 1]`.
- Updating the best length at each step.
- Correct handling of empty arrays, one-element arrays, duplicates, and decreasing arrays.
- Time and space complexity.
### Follow-up Questions
- How would you return the start and end indices of the longest run?
- How does this differ from the longest increasing subsequence?
- How would you solve the circular-array version?
- What changes if non-decreasing subarrays are allowed?
Quick Answer: Solve the longest increasing contiguous subarray problem in one pass. Includes Python code, edge cases, complexity, and a follow-up for returning the subarray range.
Return the length of the longest contiguous strictly increasing subarray.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([1,3,5,4,7],)
Expected Output: 3
Explanation: Prompt example.
Input: ([],)
Expected Output: 0
Explanation: Empty array.
Input: ([2,2,2],)
Expected Output: 1
Explanation: Equal breaks run.
Input: ([1,2,3,4],)
Expected Output: 4
Explanation: Whole array.
Hints
- Pick a representation that makes the requested operation direct.
- Handle empty inputs and boundary cases first.