Maximum Suffix–Prefix Overlap
Updated
Problem statement
Given strings first and second, return the largest integer k such that the last k characters of first equal the first k characters of second.
The overlap may use an entire input string. Length 0 is always allowed. For this exercise, assume the inputs contain only lowercase English letters; an empty input therefore yields 0.
Function
maximumOverlap(first: String, second: String) → intExamples
Example 1
first = "ababc"second = "bca"return = 2The longest matching suffix and prefix are both bc, of length 2.
Example 2
first = "aaaa"second = "aa"return = 2The full second string can overlap, so the answer is 2.
Example 3
first = "cat"second = "dog"return = 0No nonempty suffix-prefix pair agrees.
Constraints
0 <= first.length, second.length <= 2 × 10^4.- Both strings contain only lowercase English letters.
More Cresta interview questions
- Decode Words with Anagram KeysOA · Seen Oct 2026
- Find a File in a Directory SnapshotOA · Seen Oct 2026
- Leaves Remaining After WindOA · Seen Oct 2026
- Particle Moves to TargetOA · Seen Jul 2026
- Rightward Bacteria ConsumptionOA · Seen Jul 2026
- Top Three Companies by ProfitOA · Seen Jul 2026
- Peak Chat Load Across AgentsPHONE SCREEN · Seen Nov 2026
- File Storage ServicePHONE SCREEN · Seen Nov 2026
- Dynamic Least-Loaded Job DispatcherPHONE SCREEN · Seen Oct 2026
- Feature Flag Evaluation EngineONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- REST API: Country CodesHackerRank · OAHackerRank interview questions
- Fill One Exact-Length Binary WindowAmazon · OA
- Remove Vowels from a StringHSBC · OAHSBC interview questions
- Branch with the Most FilesCapital One · OACapital One interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Robot Stops Left or Right (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions