← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google coding interview, one algorithmic problem on subsequences. Pretty sparse on details but the problem itself is a classic dynamic programming type that can trip you up if you haven't seen it.

Questions Asked (1)

Q1

Find the longest alternating subsequence in an array.

Algorithms & Data Structures
Author's notes

I knew there was a DP angle here but spent too long trying to build a full table before realizing the greedy approach is cleaner.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the definition of an alternating subsequence (e.g., differences alternate in sign) and whether the subsequence must be contiguous. Then, discuss a dynamic programming approach that tracks the longest alternating subsequence ending at each index with either a positive or negative difference, optimizing to O(n) time and O(1) space by observing that the length increases only at local extrema.

Pro tip: Mention that the problem reduces to counting the number of sign changes in the sequence of differences between consecutive elements after removing duplicates, which can be done in a single pass. This shows deep insight and can lead to a very clean solution.

1. Clarify the problem

Ask whether the subsequence must be contiguous and confirm the definition of alternating (e.g., differences alternate between positive and negative). Also, check if equal elements are allowed and how they affect alternation.

2. Discuss brute force and DP

Explain a brute force approach (exponential) and then a dynamic programming approach that uses two arrays (up and down) to track the longest alternating subsequence ending at each index with the last difference positive or negative.

3. Optimize to O(n) time and O(1) space

Show that the DP can be optimized by only keeping track of the last two values and the current lengths, or by counting sign changes in the differences between consecutive elements after removing duplicates.

4. Handle edge cases

Discuss edge cases such as empty array, single element, all equal elements, and strictly increasing/decreasing arrays. Explain how the algorithm handles them.

5. Analyze complexity and test

State the time and space complexity (O(n) time, O(1) space) and walk through a small example to verify correctness.

Key Points to Mention

  • Definition of alternating subsequence: differences alternate in sign (e.g., a < b > c < d).
  • Dynamic programming with two states: up[i] and down[i] representing the longest alternating subsequence ending at i with the last difference positive or negative.
  • Optimization: The length increases only at local extrema, so we can count sign changes in the difference array.
  • Time complexity: O(n) with a single pass; space complexity: O(1) if optimized.
  • Edge cases: empty array, single element, all equal elements, and monotonic arrays.
  • Comparison with related problems like longest alternating subarray (contiguous) and longest increasing subsequence.

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