Find Longest Subsequence Common To X As Substring In Y

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

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

Salesforce hit you with a DP string problem in March 2024, and the wording is deliberately tangled. You need to find the longest subsequence of X that appears as a substring (contiguous) in Y. This is not LCS. The twist: a subsequence of X can skip characters, but once you're building it, those characters must sit together in Y. You're bridging two different matching rules, and that's where candidates blank. StealthCoder catches this pattern and walks you through the state transition when you're live.

Pattern and pitfall

The algorithm uses DP with two pointers or a 2D table tracking position in X and position in Y. For each character in X, you try to extend matches in Y as a contiguous block. The key insight: you're not finding matching characters scattered across Y, you're looking for runs in Y that align with a carefully chosen subsequence of X. Common trap: confusing this with longest common substring or LCS. The DP state is usually dp[i][j] representing the longest valid match using X[0..i-1] and checking Y[0..j-1]. You'll iterate through possible starting positions in Y and greedily match characters from X in order. Most candidates get stuck deciding when to reset or extend. Having a working pattern reference via StealthCoder during the OA saves 10-15 minutes of debugging.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Find Longest Subsequence Common To X As Substring In Y 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest common subsequence. If you have time before the OA, drill that.

⏵ The honest play

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

Salesfoece reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Longest Subsequence Common To X As Substring In Y FAQ

Is this the same as longest common subsequence?+

No. LCS lets both strings skip characters freely. Here, X skips freely but its matched characters must form a contiguous block in Y. That constraint flips the entire DP design. Watch for that trick in clarifications.

Should I iterate Y for all possible substrings first?+

Yes, one clean approach: for each substring of Y, check if you can build it as a subsequence of X using greedy matching. Track the longest. It's O(n^2 * m) but easier to code correctly than a single DP table.

What if X or Y is empty?+

Empty X returns 0. Empty Y returns 0. Check these edge cases early. Salesforce likes tripping up candidates on boundary conditions in string problems.

How do I avoid off-by-one errors in the DP indexing?+

Build your DP table with dp[len(X)+1][len(Y)+1] and use 1-based indexing for strings. It's slower to type but it catches most indexing bugs immediately. Test with X='a', Y='a' first.

Is there a greedy or two-pointer solution?+

Two pointers can work if you're clever: slide through Y, and at each position try to match a maximal subsequence of X greedily. But the DP version is more reliable in an interview. Greedy can miss optimal subsequences if Y has repeated characters.

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

OA at Salesfoece?
Invisible during screen share
Get it