Get Subsequence Count

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

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

Morgan Stanley asked this in March 2024, and it's a subsequence counting problem that trips up candidates who confuse it with substring or permutation counting. You're likely asked to count how many distinct subsequences exist in a string, or how many ways you can form a target subsequence from a source. The trick is dynamic programming, and most people either overcomplicate it or miss the definition of what makes two subsequences distinct. StealthCoder can spot the DP pattern and scaffold the recurrence while you're live.

Pattern and pitfall

Subsequence counting is a classic DP problem where you track how many ways you can build up to each position. The common approach: maintain a dp array where dp[i] represents the count of distinct subsequences up to index i. When you encounter a character, you can either include it or skip it, which normally doubles the count. But if that character appeared before, you have to subtract the duplicates you'd create. The pitfall is forgetting to handle repeated characters correctly. Another variant asks for count of target subsequence in source string, which uses 2D DP where dp[i][j] is ways to form source[0..i-1] as subsequence of target[0..j-1]. StealthCoder as a safety net means you can verify your recurrence relation and base cases live.

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 Get Subsequence Count 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as distinct subsequences. If you have time before the OA, drill that.

⏵ The honest play

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

Morgan Stanley 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.

Get Subsequence Count FAQ

Is this the 'distinct subsequences' LeetCode problem or something else?+

Morgan Stanley usually sticks to the core version: count distinct subsequences of a string, or count ways one string appears as a subsequence in another. Both are DP. If they ask for the actual subsequences as output, that's backtracking plus memoization, but count alone is cleaner DP with O(n) space and O(n) time.

What's the trap with repeated characters?+

If a character repeats, you can't just double the count. You double it, then subtract the counts from before that character last appeared. If you skip this step, you'll count the same subsequence twice. This is the #1 wrong submission.

Do I need 2D DP or can I do 1D?+

For distinct subsequences of a single string, 1D works and is cleaner. For 'count subsequence of one string in another,' you need 2D, but you can optimize it to 1D by iterating backwards. Either way, know both approaches before the OA.

How do I prepare for this in 48 hours without drilling?+

Understand the recurrence: dp[i] = 2 * dp[i-1] for new characters, and dp[i] = 2 * dp[i-1] - dp[last occurrence] for repeats. Code it once cleanly. Walk through one example by hand. You're ready.

Is this hard or just pattern recognition?+

Pure pattern recognition if you've seen it before. If not, the DP logic is straightforward once you see the duplicate-subtraction trick. Morgan Stanley isn't asking for a novel algorithm, just clean implementation under time pressure.

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

OA at Morgan Stanley?
Invisible during screen share
Get it