DSA interview question
You're building a plagiarism-detection / template-matching feature for a document-review tool. Given a source document string s and a target pattern string t, you need to count how many distinct ways t can be formed by deleting some characters from s (without reordering the remaining ones). This tells the compliance team how many independent "occurrences" of a suspicious phrase pattern exist within a larger blob of text, even when the phrase is interleaved with other content.
Formally: given strings s and t, return the number of distinct subsequences of s which equal t.
int numDistinct(string s, string t);
s | t | Output | Explanation |
|---|---|---|---|
"rabbbit" | "rabbit" | 3 | 3 ways: choosing different b's at position 3,4, or 5 to skip |
"babgbag" | "bag" | 5 | 5 distinct index-subsequences spell "bag" |
"abc" | "abc" | 1 | only one way — pick every character |
"abc" | "" | 1 | empty string is a subsequence of everything, exactly once (delete everything) |
"" | "abc" | 0 | can't form a non-empty target from an empty source |
"aaa" | "a" | 3 | pick any one of the 3 a's |
"abc" | "abcd" | 0 | t longer than s, impossible |
(Note: counts can get large — this maps naturally to unsigned long long or int per LeetCode constraints, but mention overflow awareness.)
t appears as a subsequence (not substring) of s."s and t guaranteed non-empty, or do I need to handle empty-string edge cases?s and t?t could contain wildcard characters (like . matching any character)?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in