Longest Subsequence That Is a Substring

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

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

The mistake that sinks a first attempt on this Wells Fargo OA, reported in July 2026, is mixing up which string needs to be contiguous. Candidates run a classic LCS and get the wrong answer. You get x and y, and you need the longest piece of y, taken as a substring, that also fits inside x as a subsequence. It's a dynamic programming problem with 2000-length strings, so O(n*m) is fine. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time as a safety net.

The problem

Given two strings x and y, return the maximum length of a subsequence of x that is also a contiguous substring of y.
A subsequence keeps the relative order of selected characters but may delete characters. A substring uses consecutive characters.

Function
longestSubsequence(x: String, y: String) → int

Examples
Example 1
x = "abcd"
y = "abdc"
return = 3
"abd" is a subsequence of x and a contiguous substring of y, so the answer is 3.
Example 2
x = "hackerranks"
y = "hackers"
return = 7
"hackers" appears contiguously in y and can be selected in order from x.
Example 3
x = "abc"
y = "aedace"
return = 2
"ac" is a subsequence of x and a substring of y. No qualifying value has length 3.

Constraints
1 <= x.length, y.length <= 2000.
x and y contain only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: fix a start index i in y, then greedily match y[i], y[i+1], and so on against x with a single pointer moving forward. The matched length is the longest substring of y starting at i that is a subsequence of x. Take the max over all i. That's O(m*n) worst case and needs no table. The DP version is dp[i][j] = longest substring of y ending at j that is a subsequence of x[0..i]. If x[i]==y[j], dp[i][j]=dp[i-1][j-1]+1, else dp[i][j]=dp[i-1][j]... careful, that second case is where people go wrong. Carrying the previous row's value breaks contiguity in y. Two-pointer greedy avoids that trap entirely. Check example 3: x=abc, y=aedace gives 2 from ac. Edge case: no shared characters returns 0. If the live OA freezes you, StealthCoder is the hedge that reads the prompt and hands you the working approach.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Longest Subsequence That Is a Substring 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Wells Fargo 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.

Longest Subsequence That Is a Substring FAQ

What's the trick for Longest Subsequence That Is a Substring?+

Only y needs to be contiguous. For each start index in y, walk forward and greedily match its characters in order against x. Count how far you get before a character can't be matched. The best count across all starts is your answer. No full DP table is needed.

Is this just longest common subsequence?+

No. LCS lets both strings skip characters. Here the piece taken from y must be consecutive. Running standard LCS overcounts. Example 1 shows it: x=abcd, y=abdc gives 3 here, but you can't take non-adjacent chars from y to inflate the answer.

Will O(n*m) pass the constraints?+

Yes. Both strings are up to 2000 characters, so about 4 million steps at worst. That's comfortable. The greedy per-start scan uses O(1) extra space. You don't need anything fancier like suffix automata or binary search.

What edge cases should I test?+

Test no shared characters, which returns 0. Test y fully contained in x as a subsequence, like example 2 returning 7. Test repeated letters, where greedy matching must advance x's pointer past the used character. Also test single-character strings on both sides.

How do I prepare for this in 48 hours?+

Write the greedy two-pointer solution once from scratch and run the three given examples. Then write the DP form dp[i][j] and see why carrying a value across mismatches breaks contiguity. Practice explaining the difference between subsequence and substring out loud. That covers most variants.

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

OA at Wells Fargo?
Invisible during screen share
Get it