Longest Common Subsequence Length
Reported by candidates from Zscaler's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zscaler reported this one in June 2024, and it's the classic longest common subsequence question with no disguise. Strip the wording and it's a 2D grid of prefix comparisons, where each cell asks one thing: do these two characters match? If you've got a Zscaler OA coming, expect to write a dynamic programming table in a few minutes, not invent anything new. Two strings up to 1000 characters, return an int. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank on the recurrence mid-assessment, but the idea is small enough to hold in your head.
The problem
Given two lowercase strings first and second, return the length of their longest common subsequence. A subsequence is formed by deleting zero or more characters without changing the relative order of the remaining characters. A common subsequence appears in both strings; its characters do not need to be consecutive. Function longestCommonSubsequence(first: String, second: String) → int Examples Example 1 first = "abcde" second = "ace" return = 3 The subsequence ace appears in both strings. Example 2 first = "abc" second = "def" return = 0 The strings have no character in common. Example 3 first = "" second = "abc" return = 0 An empty string has no nonempty subsequence. Constraints 0 <= first.length, second.length <= 1000 Both strings contain only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The problem reduces to a table dp[i][j], the LCS length of the first i characters of first and the first j characters of second. If first[i-1] equals second[j-1], then dp[i][j] = dp[i-1][j-1] + 1. Otherwise it's max(dp[i-1][j], dp[i][j-1]). Row 0 and column 0 are all zeros, which handles the empty string example for free. With lengths up to 1000, O(n*m) is about a million cells, which is fine. The common pitfall is confusing subsequence with substring and resetting to zero on a mismatch. Another is off-by-one indexing between the table and the strings. You can shrink memory to two rows, but it isn't required. If you blank on the recurrence during the live OA, StealthCoder is the hedge that hands you the table logic so you can type it cleanly and check the three examples.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Longest Common Subsequence Length 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest common subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Zscaler's OA.
Zscaler 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 Common Subsequence Length FAQ
How hard is the Zscaler longest common subsequence question really?+
It's a medium-level dynamic programming problem, and the most standard one there is. The recurrence is two lines. Difficulty comes from indexing mistakes, not from the idea. If you've written a 2D DP table before, this is a ten-minute job.
What's the trick to solving it?+
Build a (n+1) by (m+1) table of prefix LCS lengths. On a character match, take the diagonal value plus one. On a mismatch, take the max of the cell above and the cell to the left. The answer is the bottom-right cell.
Why not just use recursion?+
Plain recursion branches on every mismatch and blows up exponentially. With strings up to 1000 characters it won't finish. Memoize on (i, j) or go bottom-up with a table. Both give O(n*m) time, and bottom-up avoids recursion depth worries.
Which edge cases should I test?+
Test the three given examples. An empty first string returns 0, and so does an empty second string. Fully disjoint strings return 0. Identical strings return their length. Repeated characters, like aaaa against aa, are worth a quick check too.
How do I prepare in 48 hours?+
Write the LCS table from memory twice, once with a full 2D array and once with two rolling rows. Then do edit distance, since it uses the same grid shape. Don't read theory. Type it, run the examples, and fix your indexing.