Longest Alternating Up-Down Subsequence of Daily Metric Changes

Last updated: November 29, 2026

Quick Overview

Given a series of daily active user counts, find the length of the longest subsequence (not necessarily contiguous) whose successive differences strictly alternate between positive and negative.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a series of daily active user counts, find the length of the longest subsequence (not necessarily contiguous) whose successive differences strictly alternate between positive and negative.

A Google phone screen problem that rewards seeing a two-state DP where an O(n^2) subsequence DP looks necessary. Interviewers check that you can justify why only the last element's direction matters.

What the Interviewer Expects
  • Define up and down states for subsequences ending on a rise or a fall
  • Show the O(n^2) DP first, then compress
  • Explain why equal neighbors never help
  • Give O(n) time and O(1) space
  • Handle short inputs
Key Topics to Cover
1D dynamic programming
Wiggle subsequence
State compression
Greedy equivalence
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the subsequence itself.
  • Require every difference to exceed a threshold d in magnitude.
  • Count the number of longest alternating subsequences.
  • What if the input is a stream and you must answer after each value?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list x of integers. A subsequence is alternating if consecutive differences are nonzero and switch sign each time. A single element or two different elements count. Output the maximum length....

High-Level Design

Let up be the longest alternating subsequence seen so far that ends with a rise, and down the longest that ends with a fall. When x[i] > x[i-1], any subsequence ending in a fall can be extended ...


Submit Your Answer
Markdown supported