Longest Overlap Between the End of One String and the Start of Another
Company: Cresta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Given two strings, return the length of the longest suffix of the first string that is also a prefix of the second. The problem tests precise string indexing, the direction of the overlap, and boundary cases such as no overlap at all or an overlap covering the whole shorter string.
Read the full Cresta Software Engineer interview experience this question came from
Constraints
- 1 <= len(a) <= 10^4
- 1 <= len(b) <= 10^4
- a and b consist of lowercase English letters
Examples
Input: ('abcde', 'cdefg')
Expected Output: 3
Explanation: Source Example 1: the last three characters of a, 'cde', start b; k = 4 ('bcde' vs 'cdef') and k = 5 fail.
Input: ('hello', 'lohel')
Expected Output: 2
Explanation: Source Example 2: 'lo' ends a and starts b; k = 3 ('llo' vs 'loh') and longer fail.
Hints
- An overlap of length k compares exactly two pieces: the last k characters of `a` and the first k characters of `b`. Nothing else in the strings matters for that k.
- More than one k can work at the same time (think of repeating patterns such as `abab`); you need the largest one, and it may be the whole shorter string.
- Both strings can be 10^4 characters long; think about how to avoid restarting the comparison from scratch for every candidate k.