Reported September 2026
Amazondynamic programming

Longest Common Subsequence Length

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

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

The mistake that sinks a first attempt on this one is treating "subsequence" like "substring" and hunting for matching runs. Amazon candidates reported this Longest Common Subsequence Length question in September 2026, and it's a classic dynamic programming problem. Two lowercase strings, up to 1000 characters each, return one integer. If you've seen the 2D table before, it's ten minutes of work. If you haven't, it's easy to flail. StealthCoder is the safety net if you blank during the live OA, but the pattern below is short enough to memorize tonight.

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. The characters in a common subsequence must appear in both strings in the same relative order; they do not need to occupy consecutive positions.

Function
longestCommonSubsequence(first: String, second: String) → int

Examples
Example 1
first = "abcde"
second = "ace"
return = 3
The string ace appears in both inputs in the same relative order.
Example 2
first = "abc"
second = "def"
return = 0
The two strings share no character, so the longest common subsequence is empty.

Constraints
0 <= first.length, second.length <= 1000
first and second contain only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a table dp where dp[i][j] is 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 dp[i][j] = max(dp[i-1][j], dp[i][j-1]). Row 0 and column 0 are all zeros, which handles empty strings for free. Answer is dp[m][n]. The common pitfall is greedy matching, like scanning for the first shared character, which fails on cases like "abcde" and "ace" the moment order gets tricky. The second pitfall is off-by-one indexing between the table and the strings. With 1000 by 1000 you get about a million cells, which is fine. You can drop to two rows for O(min(m,n)) space. If your mind goes blank mid-assessment, StealthCoder runs invisibly and can hand you the recurrence while you type.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest common subsequence. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Common Subsequence Length FAQ

What's the trick to Longest Common Subsequence Length?+

Use a 2D DP table. If the current characters match, take the diagonal value plus one. If not, take the max of the cell above and the cell to the left. Initialize the first row and column to zero. The answer sits in the bottom-right cell.

How hard is this one really for an Amazon OA?+

It's a standard medium. The recurrence is short, but you have to know it or derive it quickly. Candidates who've never seen 2D DP struggle. Candidates who have usually finish fast. Edge cases are empty strings, which the zero-filled table handles automatically.

Can I solve it with recursion and memoization instead?+

Yes. Define f(i, j) on string suffixes or prefixes, cache results in a 1001 by 1001 grid, and use the same match or max logic. It works within the constraints, but deep recursion can hit stack limits in some languages. The bottom-up table is safer.

Is the subsequence versus substring difference a real trap?+

Yes. A substring must be contiguous. A subsequence only needs to keep relative order. In the example, "ace" is a valid common subsequence of "abcde" even though the letters aren't adjacent. If your solution compares consecutive runs, it's solving the wrong problem.

How do I prepare for this in 48 hours?+

Write the table solution from scratch three times without looking. Trace Example 1 by hand to see the grid fill. Then do the two-row space optimization once. Also test the empty-string case and the no-shared-characters case. That covers nearly everything this question can throw at you.

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

OA at Amazon?
Invisible during screen share
Get it