Find Num of Good Subsequences
Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Infosys asked this in April 2024, and it's a dynamic programming problem disguised as a subsequence counter. You're given a sequence and need to count subsequences that meet some "good" criteria, which almost always means hitting a sum, difference, or property constraint. The trap is that brute force enumeration (2^n subsets) will time out. StealthCoder will catch the recurrence relation if you blank on the exact DP state during your live OA.
Pattern and pitfall
The pattern is classic DP with modular arithmetic. You typically define dp[i][j] as the count of subsequences up to index i with property j (often a sum or remainder). The trick is recognizing that you can't just count all subsequences; you need to filter by a constraint, usually modulo or a target value. The gotcha: updating the DP table correctly when you include vs. exclude the current element, and avoiding off-by-one errors on the constraint boundary. StealthCoder reading your problem statement in real time will help you lock the DP state definition before you code, saving 10-15 minutes of debugging.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Find Num of Good 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count different palindromic subsequences. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Infosys's OA.
Infosys reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Num of Good Subsequences FAQ
What makes a subsequence 'good' in problems like this?+
Usually a sum target, a modulo condition, or a difference constraint. Infosys tends to use modulo or exact sum. The problem statement will define it; don't assume. If you see 'good subsequences', think: what property filters the set?
Is this really a DP problem or can I use memoization with recursion?+
Both work, but iterative DP is safer in a live OA. You avoid stack overflow on large inputs and it's easier to debug. Start with the DP table, not the recursive tree.
How do I avoid the modulo trap?+
If the constraint involves a modulo (e.g., sum % k == target), your DP state must track the remainder, not the full sum. Define dp[i][r] where r is the remainder. Update carefully when you add a new element.
What's the time complexity I'm aiming for?+
O(n * m) where n is the sequence length and m is the constraint space (sum range, modulo value, or property count). This beats 2^n by a huge margin and will pass Infosys limits.
If I blank on the DP transition, how do I recover fast?+
Write out a tiny example by hand (3-4 elements) and trace the DP table. See what each state represents. The transition will click. In a live OA, this takes 3-5 minutes and saves false starts.