Group Transitive Synonyms by Four-Token Context
Updated
Problem statement
You are given an array of sentences. Split each sentence on whitespace and compare tokens exactly, including case and punctuation.
An occurrence of a word has a complete four-token context when it has at least two tokens before it and at least two tokens after it. Its context is the ordered tuple consisting of those two preceding tokens followed by those two following tokens. Two distinct words are directly synonymous if they occur with an identical complete context.
Synonymy is transitive. Return every connected synonym group containing at least two distinct words. Sort each group lexicographically, then sort the groups lexicographically by their full word lists.
Function
groupContextSynonyms(sentences: String[]) → String[][]Examples
Example 1
sentences = ["we really like cats very much","we really adore cats very much","people often adore music at work","people often love music at work"]return = [["adore","like","love"]]like and adore share one context, while adore and love share another. Transitivity joins all three words.
Example 2
sentences = ["red blue cat x y","red blue dog x y","m n run p q","m n jog p q"]return = [["cat","dog"],["jog","run"]]The two independent contexts create two groups. Their sorted word lists determine the group order.
Example 3
sentences = ["cat x y z","dog x y z"]return = []The differing first tokens do not have two tokens before them, so they cannot create a direct relation.
Constraints
0 <= sentences.length <= 50000.- Each sentence contains at most 200 whitespace-separated tokens.
- The total number of tokens is at most 1000000.
- Each token is non-empty and contains no whitespace.
- Token comparison and lexicographic ordering are case-sensitive.
More Glean Phone Screen questions
Similar questions at other companies
- Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREENStripe interview questions
- Sentence Equivalence with SynonymsRead AI · PHONE SCREEN
- Count Distinct Word MeaningsDuolingo · OADuolingo interview questions
- Numbers Station Message DecoderOptiver · OAOptiver interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Fuzzy Word Lookup With One Misspelled LetterMeta · PHONE SCREENMeta interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions