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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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 ...