Count the Number of Good Subsequences
A medium-tier problem at 51% community acceptance, tagged with Hash Table, Math, String. Reported in interviews at TuSimple and 3 others.
Count the Number of Good Subsequences is a medium-difficulty combinatorics problem that drops into assessments at companies like TuSimple, UKG, Palantir, and Nvidia. If you haven't drilled counting problems with modular arithmetic, this one will stall you live. The acceptance rate sits around 50 percent, which means half the candidates walking in either know the pattern or they don't. You either spot that this is a dynamic programming counting problem using a hash table to track state, or you spend 30 minutes backtracking through a brute force that times out. StealthCoder surfaces the working solution invisible to the proctor if you blank on the approach.
Companies that ask "Count the Number of Good Subsequences"
Count the Number of Good Subsequences is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe trick here is recognizing that you're not generating subsequences, you're counting them. Candidates typically try to build or enumerate subsequences directly, which explodes exponentially. The right move is DP with a hash table tracking how many valid subsequences end at each distinct element you've seen, updating counts using combinatorics as you iterate. Modular arithmetic keeps the numbers manageable. The pattern combines Hash Table lookups, Math (binomial coefficients or precomputed factorials), and Counting logic into one clean state machine. Most solutions iterate through the string once, updating a memo dict on the fly. If you haven't seen this class of problem before, the jump from 'generate all subsequences' to 'count them with DP' is steep. This is exactly where StealthCoder is your safety net during the live assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count the Number of Good Subsequences recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count the Number of Good Subsequences interview FAQ
Is this really asked at Palantir and Nvidia?+
Yes. Both appear in the company report for this problem. Counting and combinatorics problems are common in fintech and chip design roles where optimization and efficient enumeration matter. Expect it as a medium-difficulty screening or phone-stage problem.
Why does the obvious subsequence generation approach fail?+
Generating all subsequences is O(2^n), which times out on any reasonably sized input. The insight is that you don't need to build them, you count them using DP and combinatorics. Hash Table stores state, Math handles the counting formula, and you iterate once.
What's the relationship between Hash Table and Counting here?+
The hash table tracks how many subsequences exist ending at each distinct character or state. As you iterate, you update counts using combinatorial logic (usually binomial coefficients). Hash lookups are O(1), so the whole algorithm stays polynomial.
Do I need to precompute factorials for modular arithmetic?+
Usually yes. Most solutions precompute factorials and inverse factorials modulo some prime to handle binomial coefficient calculations on the fly. Without it, your solution gets slower or more fragile. Check the constraints to see if you can fit precomputation into time and space budgets.
How does this problem differ from other medium combinatorics problems?+
Most combinatorics problems ask you to generate or find something specific. This one asks you to count, which forces you to think in terms of state and transitions rather than enumeration. That's the conceptual jump that catches people who haven't seen the pattern before.
Want the actual problem statement? View "Count the Number of Good Subsequences" on LeetCode →