Count Subsequences
Reported by candidates from BNY Mellon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
BNY Mellon sent you a counting subsequences problem in August, and you're probably wondering if it's a DP trap or a math trick. The OA is in 36 hours. Subsequence problems almost always live in two places: dynamic programming or combinatorics. StealthCoder will catch the pattern the moment you see the full problem statement, so you're safe there. But knowing the landscape now means you won't freeze when the constraints hit.
Pattern and pitfall
Counting subsequences typically breaks into two categories. The first is DP-driven: you build a table tracking how many distinct or valid subsequences exist up to each position, often with a rolling sum or hash to avoid duplicates. The second is math-driven: combinatorics plus modular arithmetic, especially if the problem asks for answers modulo 10^9+7. The common pitfall is overthinking the uniqueness constraint. If the problem allows repeats, it's often a closed-form formula. If duplicates matter, you need a hash or DP state that tracks seen values. During the live OA, if you blank on the DP recurrence, StealthCoder will show you the state transition in seconds. The trick is always the same: define what you're counting at each step, then decide if the answer compounds additively or multiplicatively.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Count Subsequences 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as distinct subsequences. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass BNY Mellon's OA.
BNY Mellon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Subsequences FAQ
Is this a DP problem or a math problem?+
Usually both. You'll build a DP table (state = position, constraint), but the recurrence often uses math: combinations, factorials, or modular inverse. Check the constraints first. If the string length is under 100, DP is safe. If it's bigger, you need a formula.
What's the trap with duplicate characters?+
If the string has repeats (e.g., 'AAB'), naive DP counts the same subsequence twice. Use a hash map to track the last occurrence of each character, then subtract the contribution of duplicates from earlier states. This is the single most common pitfall.
How do I avoid TLE on a big string?+
If length is above 1000, you can't enumerate all subsequences. You need a closed-form recurrence or combinatorial formula. The answer often compresses to O(n) with modular arithmetic. Precompute factorials and inverses if needed.
Should I handle modulo 10^9+7?+
Probably. It's the default for counting problems that can explode combinatorially. Apply mod at every multiplication and addition to stay safe. Have your modular inverse code ready, or the problem will time out on large factorials.
Is this problem still asked after seeing it on LeetCode?+
Yes. BNY Mellon uses classic DP patterns. Seeing one variant on LeetCode doesn't mean you've seen this exact constraint set. The shape is familiar, but edge cases (empty strings, single char, all duplicates) trip people up. Spend 10 minutes on those before the OA starts.