The base case is pretty standard sliding window stuff, got through it fine.
First, clarify the problem and edge cases, then present a linear scan for the basic version, tracking the current increasing run length. For the follow-up, explain how to extend the scan by considering the effect of changing one element, using precomputed increasing run lengths from the left and right to efficiently compute the maximum length after a single modification.
Pro tip: Explicitly state the time and space complexity of your solution and discuss potential optimizations or alternative approaches, as Google interviewers value efficiency and depth of analysis.
Ask clarifying questions: Is the array non-empty? Can the array contain negative numbers? For the follow-up, can we change an element to any integer, including one already present? Confirm that 'contiguous subarray' means a contiguous segment of the array.
Propose a single-pass algorithm: iterate through the array, maintain a current length of the strictly increasing contiguous subarray, update the maximum length, and reset the current length when the increasing order breaks. State the O(n) time and O(1) space complexity.
Explain that changing one element can potentially merge two increasing runs separated by a single element. To handle this efficiently, precompute for each index the length of the increasing run ending at that index (left to right) and the length of the increasing run starting at that index (right to left).
Iterate through the array, and for each index i, consider changing nums[i] to bridge the left run ending at i-1 and the right run starting at i+1. If nums[i-1] + 1 < nums[i+1], the new length is left[i-1] + 1 + right[i+1]; otherwise, it's max(left[i-1], right[i+1]) + 1. Also consider changing the first or last element to extend a run. Keep track of the global maximum.
Walk through examples, including arrays that are already strictly increasing, arrays with all equal elements, and arrays where changing one element doesn't help. Discuss time and space complexity: O(n) time and O(n) space for the precomputed arrays, with possible optimization to O(1) space if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.