FastPrepLongest Common Substring
Problems/Longest Common Substring

Longest Common Substring

xAI logoxAI● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The assessment report names Longest Common Substring as one of three timed questions. The report does not keep the function signature or the limits, so our version uses two strings and returns only the maximum length.

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) → int

Examples

Example 1

first = "ABABC"second = "BABCA"return = 4

BABC appears consecutively in both strings.

Example 2

first = "abc"second = "def"return = 0

The 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

xAI interview experiences

  • xAI New Grad AI Coding Interview and Take-Home: 3 Experiences
All xAI interview questionsSee xAI hiring insights
Saved
class Solution {
    public int longestCommonSubstring(String first, String second) {
        // Write your code here.
    }
}
Checking account…