PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-algorithm skills and conceptual understanding of subsequences versus substrings, along with attention to algorithmic efficiency when relating non-contiguous selections to contiguous patterns.

  • medium
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Find longest subsequence of x that is a substring of y

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

Given two strings `x` and `y`, compute the **maximum length** of a string `s` such that: - `s` is a **subsequence** of `x` (can delete zero or more characters from `x` without reordering the remaining characters), and - `s` is a **substring** of `y` (must appear as a contiguous block in `y`). Return the maximum possible length. ### Example - `x = "abcd"`, `y = "abdc"` - One optimal choice is `"abd"` (subsequence of `x`, substring of `y`), so the answer is `3`. ### Sample - `x = "hackerranks"`, `y = "hackers"` - Output: `7` (because `"hackers"` is a subsequence of `x` and also a substring of `y`).

Quick Answer: This question evaluates string-algorithm skills and conceptual understanding of subsequences versus substrings, along with attention to algorithmic efficiency when relating non-contiguous selections to contiguous patterns.

Given two strings `x` and `y`, compute the **maximum length** of a string `s` such that: - `s` is a **subsequence** of `x` (you may delete zero or more characters from `x` without reordering the remaining characters), and - `s` is a **substring** of `y` (it must appear as a contiguous block in `y`). Return the maximum possible length. ### Example - `x = "abcd"`, `y = "abdc"` → answer `3`. One optimal choice is `"abd"`, which is a subsequence of `x` and a contiguous substring of `y`. ### Sample - `x = "hackerranks"`, `y = "hackers"` → answer `7`, because `"hackers"` is a subsequence of `x` and also a substring of `y`. **Approach.** Every candidate `s` is a contiguous substring `y[i..j]` of `y`. So enumerate substrings of `y` and keep the longest one that is also a subsequence of `x`. Checking whether a string `t` is a subsequence of `x` is a single left-to-right two-pointer scan of `x`. A useful pruning fact: if `y[i..j]` is NOT a subsequence of `x`, then no longer substring starting at the same `i` can be either, so you can stop extending that start index.

Constraints

  • 0 <= len(x), len(y)
  • Strings consist of printable characters (typically lowercase English letters in the examples).
  • The answer is 0 when no character of y appears in x, or when either string is empty.
  • The result never exceeds min(len(x), len(y)).

Examples

Input: ("abcd", "abdc")

Expected Output: 3

Explanation: "abd" is a subsequence of x="abcd" and a substring of y="abdc", length 3. No length-4 substring of y is a subsequence of x.

Input: ("hackerranks", "hackers")

Expected Output: 7

Explanation: The whole of y="hackers" is a subsequence of x="hackerranks" (drop the extra r, n, s), so the answer is its full length 7.

Input: ("", "abc")

Expected Output: 0

Explanation: x is empty, so the only common string is the empty string; length 0.

Input: ("abc", "")

Expected Output: 0

Explanation: y is empty, so there is no non-empty substring of y; length 0.

Input: ("aaaa", "aa")

Expected Output: 2

Explanation: y="aa" is a subsequence of x="aaaa", so the whole substring of length 2 qualifies.

Input: ("abc", "xyz")

Expected Output: 0

Explanation: x and y share no characters, so no non-empty substring of y is a subsequence of x; length 0.

Input: ("abcabc", "abcabc")

Expected Output: 6

Explanation: y equals x, and any string is a subsequence of itself, so the whole length-6 substring qualifies.

Input: ("a", "a")

Expected Output: 1

Explanation: Single matching character; the substring "a" of y is a subsequence of x, length 1.

Hints

  1. Every valid s is a contiguous substring of y. Enumerate substrings y[i..j] and test each against x.
  2. Testing whether a string t is a subsequence of x is a single two-pointer pass: walk through x once, advancing a pointer in t whenever characters match; t is a subsequence iff the pointer reaches the end of t.
  3. Pruning: if y[i..j] is not a subsequence of x, then no longer substring starting at i can be either — break out of the inner loop. Also skip any candidate whose length is not greater than the best found so far.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Sum of Weekly Maximum Costs - Salesforce
  • Solve Two OA Coding Problems - Salesforce (medium)
  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)