Quick 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.

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

Given two strings `a` and `b`, find the longest stretch at the end of `a` that is also the beginning of `b`. Return its length: the largest `k` such that the last `k` characters of `a` are the same as the first `k` characters of `b`. ### Function Signature ```python def max_overlap(a: str, b: str) -> int: ``` ### Rules - `k` ranges from 0 to `min(len(a), len(b))`, and it may equal the full length of the shorter string. - The direction matters: the overlap is always the end of `a` against the start of `b`, never the end of `b` against the start of `a`. - Characters are compared exactly, one by one. - If no `k >= 1` works, return 0. ### Constraints - `1 <= len(a) <= 10^4` and `1 <= len(b) <= 10^4` - `a` and `b` consist of lowercase English letters ### Examples **Example 1** ```text Input: a = "abcde", b = "cdefg" Output: 3 ``` The last three characters of `a`, `cde`, are the first three characters of `b`. No longer stretch works: `bcde` is not `cdef`, and `abcde` is not `cdefg`. **Example 2** ```text Input: a = "hello", b = "lohel" Output: 2 ``` The last two characters of `a`, `lo`, are the first two of `b`. With the arguments swapped the answer would be 3 (`hel`), which shows that the direction matters. **Example 3** ```text Input: a = "abc", b = "xyz" Output: 0 ``` No suffix of `a` is a prefix of `b`.

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

Given two strings `a` and `b`, find the longest stretch at the end of `a` that is also the beginning of `b`, and return its length: the largest integer `k` such that the last `k` characters of `a` are the same as the first `k` characters of `b`. Implement `max_overlap(a, b)`. **Rules** - `k` ranges from 0 to `min(len(a), len(b))`, and it may equal the full length of the shorter string. - The direction matters: the overlap is always the end of `a` against the start of `b`, never the end of `b` against the start of `a`. - Characters are compared exactly, one by one. - If no `k >= 1` works, return 0. The answer is at most 10^4, so it fits in a 32-bit `int` in every language. **Example 1** ``` Input: a = "abcde", b = "cdefg" Output: 3 ``` The last three characters of `a`, `cde`, are the first three characters of `b`. No longer stretch works: `bcde` is not `cdef`, and `abcde` is not `cdefg`. **Example 2** ``` Input: a = "hello", b = "lohel" Output: 2 ``` The last two characters of `a`, `lo`, are the first two characters of `b`. With the arguments swapped (`a = "lohel"`, `b = "hello"`) the answer would be 3 (`hel`), which shows that the direction matters. **Constraints** - `1 <= len(a) <= 10^4` - `1 <= len(b) <= 10^4` - `a` and `b` consist of lowercase English letters

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

  1. 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.
  2. 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.
  3. Both strings can be 10^4 characters long; think about how to avoid restarting the comparison from scratch for every candidate k.

Loading coding console...

Show the approach

Approach

Treat b as a pattern and a as a text and run the Knuth-Morris-Pratt automaton. First build the prefix function of b: pi[i] is the length of the longest proper prefix of b[0..i] that is also a suffix of b[0..i]. Then scan a from left to right while keeping j, the length of the longest prefix of b that is a suffix of the part of a read so far. Before consuming the next character, while j > 0 and either j == len(b) (a complete copy of b cannot be extended) or the character differs from b[j], fall back to j = pi[j - 1]; then, if the character equals b[j], increase j by one.

Invariant: after reading a[0..i], j is the largest length such that b[0..j-1] equals the last j characters of a[0..i]. The fallback chain pi[j - 1], pi[pi[j - 1] - 1], ... lists every shorter prefix of b that is still a suffix of the text in decreasing order, so the first one that the new character extends is the longest valid one and no candidate is skipped. After all of a is read, j is exactly the largest k with a[len(a) - k:] == b[:k], and it is automatically at most min(len(a), len(b)).

Edge cases: no overlap leaves j = 0; b equal to the end of a ends with j = len(b); a equal to the start of b ends with j = len(a); a copy of b in the middle of a is not reported, because the fallback at j == len(b) keeps scanning; when several k work (periodic strings) the largest wins because j always tracks the longest match; only the end of a against the start of b is measured, never the reverse.

Time complexity:
O(len(a) + len(b))
Space complexity:
O(len(b))