Longest Common Substring
Updated
Problem statement
Given strings first and second, return the length of their longest common substring.
A substring occupies consecutive positions in its original string. If the strings share no character, return 0.
Function
longestCommonSubstring(first: String, second: String) → intExamples
Example 1
first = "ABABC"second = "BABCA"return = 4BABC appears consecutively in both strings.
Example 2
first = "abc"second = "def"return = 0The strings share no character.
Constraints
0 <= first.length, second.length <= 3000.- Both strings contain only printable ASCII characters.
- An empty string has no non-empty substring.
More xAI interview questions
- Largest Plus SignOA · Seen May 2025
- Sort Concentric Matrix RingsOA · Seen May 2025
- Resumable List IteratorPHONE SCREEN · Seen Apr 2026
- Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
- Live Top-K Spaces by Active UsersONSITE INTERVIEW · Seen Jan 2026
- Total Active Time by SpaceONSITE INTERVIEW · Seen Jan 2026
- Dynamic Batch Decode TraceONSITE INTERVIEW · Seen Dec 2025
- Nested Transaction Key-Value StoreONSITE INTERVIEW · Seen Oct 2025
- Weighted LRU CacheONSITE INTERVIEW · Seen Oct 2025
- LFU CacheONSITE INTERVIEW · Seen Sep 2025
Similar questions at other companies
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Longest Subsequence That Is a SubstringBooking.com · OA
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
- Palindrome CounterGoldman Sachs · OAGoldman Sachs interview questions
- Longest String ChainVisa · OAVisa interview questions
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
- Substrings Without a Target SubsequencePhonePe · OAPhonePe interview questions
- Minimum Edit-Distance String for Each QueryTarget · OA
- Longest Subsequence That Is a SubstringWells Fargo · OAWells Fargo interview questions