← Questions

Merge known bigrams in a sentence, leftmost first

0
SpaceXAICoding

A coding interview question asked at SpaceXAI. It tests strings, hashing and greedy.

Write a function that splits a sentence into tokens using a dictionary of known two-word phrases (bigrams).

def bigram_split(text: str, bigrams: list[str]) -> list[str]:
    ...
  • text is a non-empty string of lowercase words separated by spaces.
  • bigrams is a list of phrases. Each phrase is two lowercase words separated by one space, for example "machine learning". A bigram is directed: "a b" does not match b a.

Rules:

  1. Read the words from left to right, and keep their original order in the output.
  2. When two adjacent words form a phrase in bigrams, output them as one token, with the two words joined by one space.
  3. Every other word is its own token.
  4. If two possible matches overlap, take the one that starts further left. A word can be part of only one token.

Example 1

text    = "we use machine learning models"
bigrams = ["machine learning", "learning models"]
output  = ["we", "use", "machine learning", "models"]

"machine learning" and "learning models" share the word learning. The leftmost match wins, so models stays on its own.

Example 2

text    = "go go go"
bigrams = ["go go"]
output  = ["go go", "go"]

Constraints

  • 1 <= number of words <= 10^5
  • 0 <= len(bigrams) <= 10^5
  • Words contain only the letters a-z.

Aim for time linear in the length of the input.

Clarifying questions

  • Can a word appear in more than one bigram, and do repeated entries in bigrams matter?
  • Can there be several spaces between words, or leading and trailing spaces?
  • Is the match exact, or should it ignore case?
  • After a bigram is used, does the scan continue from the word right after it?

What a strong answer covers

  • Puts the bigrams in a set of word pairs for O(1) lookups, then scans the words with an index. If words[i] and words[i + 1] form a bigram, output them together and move ahead by 2; otherwise output words[i] and move ahead by 1.
  • Explains why this greedy scan gives the "leftmost wins" rule: the earliest possible match is always taken first and its words are consumed.
  • Runs in O(number of words + total length of the bigrams).
  • Edge cases: a single word, no bigrams, repeated words (go go go), a bigram at the very end, and a bigram that appears reversed in the text.
  • Tests: both examples, a chain of overlapping bigrams (a b, b c, c d on a b c d gives ["a b", "c d"]), and a text with no matches.

Follow-ups

  • Phrases of any length (n-grams), with the longest match winning at each position: use a trie over words.
  • Choose the split that makes the most merges, instead of leftmost first (a dynamic program).
  • Phrases have scores: maximize the total score.
  • The text arrives as a stream of words.

Where it's asked

What it tests

strings · hashing · greedy · tokenization

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding