FastPrepMost Common N-Grams
Problems/Most Common N-Grams

Most Common N-Grams

xAI logoxAI● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report asks for the most common contiguous n-gram for every requested length. It leaves out how text is split into tokens, how ties break, and what to do when the window is too large. Our version supplies the usual fixed rules. It is about 90% the same as the reported question.

A document is represented as whitespace-separated tokens. Split document on one or more whitespace characters, discard empty pieces, and preserve every remaining token exactly, including case and punctuation.

For each value n = lengths[i], consider every contiguous sequence of n tokens. Return the sequence that occurs most often, joined with single spaces. If several sequences have the same maximum frequency, return the one whose first occurrence starts earliest in the document. If n exceeds the token count, return the empty string for that request.

Return one result for each entry of lengths, in the same order. Duplicate requested lengths must produce duplicate results.

Function

mostCommonNGrams(document: String, lengths: int[]) → String[]

Examples

Example 1

document = "a b a b a c"lengths = [1,2,3,7]return = ["a","a b","a b a",""]

a is the most common token. The bigrams a b and b a both occur twice, so the earlier first occurrence selects a b. The trigram a b a occurs twice.

Example 2

document = "Red  fish red fish"lengths = [1,2]return = ["fish","Red fish"]

Token matching is case-sensitive, so fish occurs twice while Red and red are distinct. All bigrams tie, selecting the first one, Red fish.

Constraints

  • 1 <= document.length() <= 200000.
  • The document contains at least one non-whitespace token.
  • 1 <= lengths.length <= 100.
  • 1 <= lengths[i] <= 1000.
  • Across distinct requested lengths, the sum of n * (tokenCount - n + 1) for every n <= tokenCount is at most 1000000.

More xAI interview questions

  • Resumable List IteratorPHONE SCREEN · Seen Apr 2026
  • Earliest Dataset Readiness in a PipelinePHONE SCREEN · Seen Mar 2026
  • Double-Base PalindromesPHONE SCREEN · Seen Oct 2024
  • 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
  • Cached Lazy Token BucketsONSITE INTERVIEW · Seen Sep 2025

Similar questions at other companies

  • Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
  • Longest Substring Without Repeating CharactersNykaa · PHONE SCREENNykaa interview questions
  • Longest Substring Without Repeating CharactersIBM · PHONE SCREENIBM interview questions
  • Longest Substring Without Repeating CharactersTikTok · PHONE SCREENTikTok interview questions
  • Find Word-Multiset WindowsAmazon · PHONE SCREEN
  • Find the First Contiguous HaikuFaire · PHONE SCREEN
  • Longest Substring Without Repeating CharactersCapgemini · PHONE SCREENCapgemini interview questions
  • Longest Substring Without Repeating CharactersWex · PHONE SCREEN
  • Minimum Window of Required WordsSalesforce · PHONE SCREENSalesforce interview questions
  • Longest Substring Without Repeating CharactersRedNote · PHONE SCREEN

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 String[] mostCommonNGrams(String document, int[] lengths) {
        // Write your code here.
    }
}
Checking account…