Reported July 2025
Googledynamic programming

Count Subsequences Without Three Equal-Parity Elements in a Row

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

The edge case that kills the naive solution here is the run length. Google's OA, reported in July 2025, asks you to count subsequences of up to 10^5 numbers where no three selected values in a row share parity. Brute force over index sets is dead on arrival. If you track only the last parity, you'll accept runs of three and get example 2 wrong (6, not 7). This is a dynamic programming problem with a tiny state. If you freeze mid-assessment, StealthCoder runs invisibly as a safety net and hands you the transition table. Know the state and you won't need it.

The problem

You are given an integer array nums. Count its non-empty subsequences whose selected order satisfies both rules:
No three consecutive selected values are all even.
No three consecutive selected values are all odd.
A subsequence is formed by deleting zero or more elements without changing the order of the remaining elements. Two subsequences are different when they select different index sets, even if their value sequences are equal.
Return the count modulo 10^9 + 7.

Function
countValidSubsequences(nums: int[]) → int

Examples
Example 1
nums = [1,2,3]
return = 7
All seven non-empty subsequences are valid because none contains three consecutive selected values of the same parity.
Example 2
nums = [2,4,6]
return = 6
The only invalid subsequence selects all three values, producing a run of three evens. The other six non-empty subsequences are valid.
Example 3
nums = [1,3,5,7]
return = 10
Every selected value is odd, so a valid subsequence may contain only one or two elements. There are 4 + 6 = 10 such index sets.

Constraints
1 <= nums.length <= 10^5.
1 <= nums[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a DP over elements where the state is the parity of the last selected value and how long that parity run is, either 1 or 2. That gives four counters: odd run 1, odd run 2, even run 1, even run 2. For each number, compute new counts from the old ones. Starting a fresh subsequence adds 1 to run 1 of its parity. Extending a subsequence ending in the opposite parity (any run length) lands in run 1. Extending the same parity with run 1 lands in run 2. Same parity with run 2 is forbidden, so you drop it. Always keep the old counts too, since skipping the element is allowed. Apply the modulo 10^9 + 7 on every addition. The common pitfall is updating counters in place and reusing a value you just changed. Snapshot the old values first. The answer is the sum of all four counters. It runs in O(n) time and O(1) space. StealthCoder is your hedge if the state design slips under pressure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Subsequences Without Three Equal-Parity Elements in a Row 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Subsequences Without Three Equal-Parity Elements in a Row FAQ

How hard is this Google OA question really?+

Medium. The idea is a small-state DP, which most candidates have seen. The difficulty is defining the state cleanly and avoiding in-place update bugs. If you can say the four counters out loud, the code is about fifteen lines.

What's the trick to solving it fast?+

Track subsequences by last parity and current run length (1 or 2). Each new number either starts a new subsequence, extends an opposite-parity one into run 1, or extends a same-parity run 1 into run 2. Same-parity run 2 is blocked.

Why does my solution fail example 2?+

You're probably tracking only the last parity, not the run length. For [2,4,6] that allows the full triple of evens and returns 7 instead of 6. You need to know whether the last two selected values matched in parity.

Do I need to worry about overflow or the modulo?+

Yes. Counts grow exponentially with 10^5 elements, so take modulo 10^9 + 7 after every addition. In languages with fixed-width ints, use 64-bit for intermediate sums. Only parity matters, so the large values in nums are irrelevant.

How do I prepare for this in 48 hours?+

Practice two or three DP problems where the state is last choice plus a small run counter. Write the transitions on paper first, then code. Test against the three examples, especially [2,4,6] and [1,3,5,7], before you submit.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it