Find Max Length of Subsequence
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's March 2024 OA included a subsequence optimization problem with no obvious greedy path. You're looking at a constraint puzzle where the naive answer fails, and the real solution hinges on dynamic programming or careful state tracking. The trick is figuring out what state actually matters. If you blank on the pattern live, StealthCoder reads the problem and surfaces the state transitions in real time so you're not guessing.
Pattern and pitfall
Max length subsequence problems almost always hinge on DP: you track the longest valid subsequence ending at each position, then decide whether to extend or skip. The hard part is identifying what constraint limits your choice. Google loves problems where greedy fails because it misses an optimal skip. You'll likely need to store not just the length but a key property (like the last element value, or a parity bit, or a sum mod N) to decide if you can extend. The common pitfall is trying to greedily take every element that fits, then realizing you've blocked a longer chain later. Build a DP table where dp[i] is the max length using elements up to index i, and track what condition gates the transition. StealthCoder handles the state design if you freeze.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find Max Length of Subsequence cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest increasing subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Max Length of Subsequence FAQ
Is this asking for a contiguous substring or any subsequence?+
The title says subsequence, which means non-contiguous elements in order. That's why greedy fails and DP is required. You need to decide at each position whether adding that element maintains validity.
What's the constraint that makes the max length non-trivial?+
Without the problem text, you're likely facing a condition like 'no two elements within distance K', 'sum must not exceed X', or 'adjacent elements in the original array can't both be chosen'. Test the examples to reverse-engineer it.
How do I code this in 15 minutes if I blank?+
Start with a DP array where dp[i] = longest valid subsequence using elements 0 to i. For each position, check if you can extend any valid predecessor state. Iterate through predecessors and track the best. It's O(n squared) but correct.
Will this solution time out?+
If n is under 1000, O(n squared) DP is fine. If it's larger, you may need segment trees or monotonic deques to speed up the max query across predecessors. Google sometimes asks both; solve the DP version first.
How is this different from LCS or LIS?+
LIS is unconstrained. This problem adds a rule that eliminates some choices. That rule is the key. Identify it from the examples, then modify your DP transition to enforce it.