FastPrepLongest Subsequence That Is a Substring
Problems/Longest Subsequence That Is a Substring

Longest Subsequence That Is a Substring

Wells Fargo logoWells Fargo● MediumFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The displayed source contains one conflicting sample-output glyph; the judged task follows its maximum-length statement and explanation. The core task matches the visible source at about 99%.

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.

More Wells Fargo problems

  • Student and Course EnrollmentOA · Seen Nov 2026
  • Bond Interest Summary Above 3%OA · Seen Oct 2026
  • Count Phrases Formed by Anagram ReplacementOA · Seen Oct 2026
All Wells Fargo interview questionsSee Wells Fargo hiring insights
Saved
class Solution {
    public int longestSubsequence(String x, String y) {
      // Write your code here
    }

}
Checking account…