DSA interview question
You're building the foundational engine behind the "changelog minimizer" and "plagiarism scanner" tools you've been extending in recent tickets — but stripped down to its core ask. Given two strings text1 and text2, you need to find the length of their longest common subsequence: the longest sequence of characters that appears in both strings in the same relative order, but not necessarily contiguously. This single primitive is the backbone that Edit Distance, SCS, Min Insert/Delete, and LPS all build on top of.
int longestCommonSubsequence(string text1, string text2);
text1 | text2 | Output | Explanation |
|---|---|---|---|
"abcde" | "ace" | 3 | "ace" is a common subsequence of both |
"abc" | "abc" | 3 | identical strings → LCS is the whole string |
"abc" | "def" | 0 | no common characters at all |
"abc" | "" | 0 | LCS with empty string is always 0 |
"" | "" | 0 | trivial |
"aggtab" | "gxtxayb" | 4 | "gtab" is the LCS |
"bl" | "yby" | 1 | "b" is the only common subsequence |
(This is the canonical LeetCode 1143 formulation — nearly every other problem in this set reduces down to this one.)
text1 and text2 guaranteed non-empty?On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in