Merge known bigrams in a sentence, leftmost first
I was asked this
No reports yet. Sign in to say where you were asked it.
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]:
...
-
textis a non-empty string of lowercase words separated by spaces. -
bigramsis 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 matchb a.
Rules:
- Read the words from left to right, and keep their original order in the output.
- When two adjacent words form a phrase in
bigrams, output them as one token, with the two words joined by one space. - Every other word is its own token.
- 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
bigramsmatter? - 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]andwords[i + 1]form a bigram, output them together and move ahead by 2; otherwise outputwords[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 dona b c dgives["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
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
