DSA interview question
You're building a "near-duplicate detector" for a code-plagiarism scanner. Unlike the earlier LCS-family problems where matches could be scattered non-contiguously, here you specifically want to detect copy-pasted contiguous blocks of code between two submissions str1 and str2 — since a plagiarized chunk is, by definition, an unbroken run of matching characters, not just any matching subsequence. Given two strings, find the length of the longest common substring (contiguous in both strings).
int longestCommonSubstring(string str1, string str2);
str1 | str2 | Output | Explanation |
|---|---|---|---|
"abcde" | "abfce" | 2 | "ab" is the longest contiguous common substring |
"abcdxyz" | "xyzabcd" | 4 | "abcd" (or "xyz") — longest contiguous match is length 4 |
"zxabcdezy" | "yzabcdezx" | 6 | "abcdez" is common and contiguous in both |
"abc" | "abc" | 3 | identical strings |
"abc" | "def" | 0 | no common characters at all |
"" | "abc" | 0 | empty string has no substrings |
"aaaa" | "aa" | 2 | longest run of matching contiguous "a"s is 2 (bounded by shorter string) |
(Key contrast to flag immediately: this is NOT the same as Longest Common Subsequence — "abcde" vs "abfce" have LCS length 4 ("abce"), but longest common SUBSTRING is only 2 ("ab"). Interviewers love testing whether you conflate these two.)
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