← Salesforce Interview Insights

Salesforce·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Salesforce SWE interview with a DP problem that looked deceptively clean on the surface. The twist of combining subsequence logic with substring constraints is the kind of thing that bites you if you go in assuming it's a standard LCS.

Questions Asked (1)

Q1

Given two strings x and y, find the length of the longest subsequence of x that is also a contiguous substring of y. Optionally reconstruct the actual matching string.

Algorithms & Data Structures
Author's notes

The part that tripped me up was the asymmetry: x contributes a subsequence (characters don't have to be adjacent) but y contributes a substring (they do).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem: we need the longest subsequence of x that appears as a contiguous substring in y. A dynamic programming approach with states (i, j) representing the longest common subsequence ending at x[i] and y[j] can be used, but we must ensure the subsequence is contiguous in y. Alternatively, for each substring of y, find the longest subsequence of x that matches it, but that is inefficient. A more efficient DP: let dp[i][j] be the length of the longest common subsequence of x[0..i] and y[0..j] that ends with x[i] and y[j] (i.e., x[i] == y[j]). Then dp[i][j] = 1 + max(dp[i-1][k] for k < j where x[i-1] matches y[k]? Actually, we need the subsequence to be contiguous in y, so the previous character in the subsequence must come from y at an index less than j, but not necessarily j-1. However, since the subsequence must be contiguous in y, the characters in y that form the subsequence must be consecutive. So if we pick y[j] as the last character, the previous character in the subsequence must be y[j-1]. Therefore, the subsequence in y is a substring, so the indices in y are consecutive. Thus, if we match x[i] to y[j], the previous matched character in x must be some i' < i, and the previous matched character in y must be y[j-1]. So we need to find the longest subsequence of x[0..i-1] that matches y[0..j-1] and ends with y[j-1]? Actually, the subsequence in y is contiguous, so if we match x[i] to y[j], the previous character in the subsequence (if any) must be matched to y[j-1]. So we need to find the longest subsequence of x[0..i-1] that is a substring of y ending at j-1. This suggests a DP where we fix the start of the substring in y. Alternatively, we can think of it as: for each starting position in y, find the longest subsequence of x that matches a prefix of y starting at that position. But that would be O(n*m^2). A better DP: Let dp[i][j] be the length of the longest common subsequence of x[0..i] and y[0..j] that ends with x[i] and y[j] and is contiguous in y. Then dp[i][j] = 1 + max(dp[i-1][j-1] if x[i-1] matches y[j-1]? No, because the previous character in the subsequence must be matched to y[j-1], but x[i-1] may not match y[j-1]. Actually, the previous character in the subsequence is some x[i'] with i' < i, and it must match y[j-1]. So we need to consider all i' < i such that x[i'] == y[j-1] and take the max dp[i'][j-1]. So dp[i][j] = 1 + max_{i' < i, x[i'] == y[j-1]} dp[i'][j-1]. This can be computed efficiently by maintaining for each character the maximum dp value for that character at previous j. So overall O(n*m) time. Then the answer is max over i,j of dp[i][j]. To reconstruct, we can store parent pointers. This is a good approach to present.

Pro tip: Start by clarifying the problem with the interviewer: confirm that the subsequence must be contiguous in y but not necessarily in x, and that we need the length (and optionally the string). Then discuss the DP state and transition, emphasizing the contiguity constraint in y. Mention that a naive approach would be O(n*m^2) but we can optimize to O(n*m) by maintaining the best previous match for each character.

1. Clarify the problem

Restate the problem to ensure understanding: find the longest subsequence of x that is also a contiguous substring of y. Ask if the subsequence must be contiguous in x (no) and if we need to return the string or just the length.

2. Define DP state and transition

Define dp[i][j] as the length of the longest common subsequence of x[0..i] and y[0..j] that ends with x[i] and y[j] and is contiguous in y. Transition: dp[i][j] = 1 + max_{i' < i, x[i'] == y[j-1]} dp[i'][j-1] if x[i] == y[j], else 0.

3. Optimize transition

To avoid O(n) per state, maintain for each character c an array best[c][j] = max_{i' < i, x[i'] == c} dp[i'][j]. Then dp[i][j] = 1 + best[x[i]][j-1] if x[i] == y[j]. Update best after computing dp[i][j].

4. Compute answer and reconstruct

The answer is the maximum dp[i][j] over all i,j. To reconstruct, store parent pointers (i', j-1) for each dp[i][j] and trace back from the maximum.

5. Analyze complexity and edge cases

Time O(n*m), space O(n*m) for dp and parent, or O(m) if only length needed with rolling arrays. Discuss edge cases: empty strings, no common characters, etc.

Key Points to Mention

  • Dynamic programming with state (i, j) representing the longest valid subsequence ending at x[i] and y[j].
  • Contiguity in y means the previous character in the subsequence must match y[j-1], so we need to consider all previous matches in x.
  • Optimization using best[c][j] to achieve O(n*m) time by avoiding linear scan for each state.
  • Reconstruction using parent pointers or by storing the actual subsequence.
  • Space optimization: rolling arrays if only length is needed, but parent pointers require full table.
  • Edge cases: empty strings, no common characters, and when x or y is very large.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.