← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google coding round with two related array problems. The follow-up was the real test and I'm not sure I handled it cleanly.

Questions Asked (2)

Q1

Given an array of integers, find the length of the longest strictly increasing contiguous subarray.

Algorithms & Data Structures
Author's notes

Pretty standard sliding window type problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and edge cases, then propose a single-pass O(n) solution that tracks the current increasing run length and updates the maximum. Walk through a small example to validate the logic before coding.

Pro tip: Explicitly state that you would handle empty arrays and single-element arrays, and mention that the solution is optimal because any algorithm must examine each element at least once.

1. Clarify requirements and edge cases

Confirm that 'contiguous subarray' means a contiguous slice of the original array, and ask about empty arrays, single elements, and negative numbers. State assumptions clearly.

2. Propose an efficient approach

Explain that a single pass suffices: maintain the length of the current strictly increasing run and the maximum seen so far. This gives O(n) time and O(1) space.

3. Walk through an example

Use a small array like [1,2,3,1,2] to demonstrate how the current run resets when the increasing condition breaks, and how the maximum is updated.

4. Write clean code

Implement the algorithm with clear variable names, handle edge cases (empty array returns 0), and avoid off-by-one errors in the loop.

5. Analyze complexity and test

State that time complexity is O(n) and space is O(1). Suggest testing with edge cases like empty array, all increasing, all decreasing, and duplicates.

Key Points to Mention

  • Single-pass O(n) time and O(1) space solution
  • Handling of edge cases: empty array, single element, all increasing, all decreasing
  • Strictly increasing condition: nums[i] > nums[i-1]
  • Resetting the current run length when the condition fails
  • Updating the maximum length at each step
  • Optimality: any algorithm must examine each element at least once

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Follow-up: you can change exactly one element in the array. What is the longest increasing contiguous subarray you can achieve?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where it got messy for me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the array can be modified by changing one element to any value, and the goal is to find the longest contiguous subarray that is strictly increasing after the change. Then, propose an O(n) solution using two passes: one to compute the length of the increasing subarray ending at each index, and another for the increasing subarray starting at each index. Finally, consider each index as the changed element and combine the lengths from adjacent segments, ensuring the change can bridge them.

Pro tip: Explicitly discuss edge cases like arrays of length 1 or 2, and mention that the changed element can be set to any value, so you only need to check if the gap between the left and right segments can be bridged by a single element. This shows attention to detail and thoroughness.

1. Clarify the problem

Confirm that 'change exactly one element' means you can replace any element with any integer, and that the subarray must be contiguous and strictly increasing. Also, clarify whether the change is mandatory (exactly one) or optional (at most one).

2. Precompute increasing runs

Compute two arrays: 'left[i]' = length of the longest increasing subarray ending at i, and 'right[i]' = length of the longest increasing subarray starting at i. This can be done in O(n) time with simple passes.

3. Consider each element as the changed one

For each index i, consider changing arr[i]. The best subarray that includes i as the changed element can combine the increasing segment ending at i-1 and the increasing segment starting at i+1, provided that arr[i-1] < arr[i+1] - 1 (so that a value can be inserted between them). Also consider subarrays that do not include i (i.e., just left[i-1] or right[i+1]).

4. Handle edge cases and compute maximum

Handle cases where i is at the boundaries (i=0 or i=n-1) and where the array length is small. Keep track of the maximum length found. Also consider the possibility of not changing any element if the problem allows 'at most one' change.

5. Analyze complexity and trade-offs

State that the solution runs in O(n) time and O(n) space, which is optimal. Discuss potential trade-offs: e.g., if space is a concern, you could use a single pass with constant space, but it's more complex.

Key Points to Mention

  • Clarify whether the change is mandatory or optional, and that the new value can be any integer.
  • Use dynamic programming to compute increasing runs from left and right.
  • When combining segments, check if arr[i-1] < arr[i+1] - 1 to ensure a valid bridge.
  • Consider subarrays that do not include the changed element (i.e., just left[i-1] or right[i+1]).
  • Handle edge cases: array length 1 or 2, and changes at the boundaries.
  • Time complexity O(n) and space complexity O(n), with possible optimization to O(1) space.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.