Reported September 2026
Applestring

Merge Two Strings by Maximum Boundary Overlap

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

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

The Apple OA reported in September 2026 looks like a string merge, and the edge case is what trips people up. You compute the suffix-prefix overlap in both orders, pick the bigger one, and break ties toward str1 first. With strings up to 10^5 characters, the obvious approach dies on time. If you've got an invite and 48 hours, learn the overlap trick and the full-containment case. StealthCoder is the safety net if you blank on the live assessment, but the idea here is small enough to hold in your head.

The problem

You are given two ASCII strings, str1 and str2. Merge them in one of the two possible orders while writing the boundary overlap only once.
For an ordered pair (first, second), its boundary overlap is the largest integer k such that:
0 <= k <= min(first.length, second.length), and
the suffix of first with length k equals the prefix of second with length k.
The merge for that order is first + second.substring(k). Compare the overlap for str1 followed by str2 with the overlap for str2 followed by str1.
Return the merge with the larger boundary overlap.
If both overlaps have the same length, return the merge with str1 first.
A boundary overlap may contain the entire shorter string.

Function
factorizeExtremities(str1: String, str2: String) → String

Examples
Example 1
str1 = "1234yyabc"
str2 = "abcxxxx1234"
return = "abcxxxx1234yyabc"
In the order str1 then str2, the longest overlap is "abc", with length 3. In the reverse order, it is "1234", with length 4. The reverse order therefore produces "abcxxxx1234yyabc".
Example 2
str1 = "abXY"
str2 = "XYab"
return = "abXYab"
Both orders have an overlap of length 2: "XY" in the first order and "ab" in the reverse order. The tie rule keeps str1 first.
Example 3
str1 = "UUUUUUUUUUUUUU"
str2 = "UUUUU"
return = "UUUUUUUUUUUUUU"
The full five-character str2 is a boundary overlap. Both directions reach length 5, so the tie rule keeps str1 first and adds no characters.

Constraints
0 <= str1.length <= 10^5.
0 <= str2.length <= 10^5.
Every character in str1 and str2 is an ASCII character.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is the prefix function (KMP failure array). To get the overlap of first's suffix with second's prefix, build the string second + separator + first and compute the prefix function. The last value is the overlap length k. Use a separator character that can't appear in the input, or cap the value at min(lengths). Do it twice, once per order, compare, and build first + second.substring(k). The naive pitfall is checking every k with substring comparison, which is O(n^2) and times out at 10^5. The edge cases bite too: empty strings, a full containment like Example 3 where k equals the shorter length, and ties going to str1 first. ASCII means a separator like a null character is fine, but verify it isn't in the input. If your mind goes blank mid-assessment, StealthCoder can supply the prefix-function code as a hedge.

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 Merge Two Strings by Maximum Boundary Overlap 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

⏵ The honest play

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

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

Merge Two Strings by Maximum Boundary Overlap FAQ

What's the trick to the Apple merge strings overlap problem?+

Use the KMP prefix function. Concatenate the second string, a separator, and the first string, then read the last prefix-function value. That's the longest suffix-of-first equal to prefix-of-second. It runs in linear time, which you need for 10^5 length inputs.

Why does the brute force approach fail?+

Trying every k and comparing substrings costs O(n) per check, so O(n^2) overall. With lengths up to 10^5 that's around 10^10 operations in the worst case. Hashing or the prefix function gets you to linear time.

What edge cases should I test?+

Test empty strings on either side, a shorter string fully contained as a boundary overlap (Example 3), equal overlaps where str1 must go first (Example 2), and all-identical characters. Also make sure your separator can't collide with input characters.

Can I use string hashing instead of KMP?+

Yes. Rolling hash compares each prefix and suffix of length k in constant time, giving linear total. KMP is deterministic and avoids collision worries, so it's the safer pick when you have to write it under pressure.

How do I prepare for this in 48 hours?+

Write the prefix function from memory twice. Then solve this problem end to end with all three examples as tests. Practice the tie rule and the empty-string cases. 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 Apple.

OA at Apple?
Invisible during screen share
Get it