Count Distinct Meanings in a Synonym List for Query Expansion
Last updated: November 29, 2026
Quick Overview
Given synonym pairs such as ("car", "auto") and a vocabulary of words, count how many distinct meanings exist, where synonymy is transitive and case-insensitive, and a vocabulary word with no synonyms is its own meaning.
0
0
0 solved
Given synonym pairs such as ("car", "auto") and a vocabulary of words, count how many distinct meanings exist, where synonymy is transitive and case-insensitive, and a vocabulary word with no synonyms is its own meaning.
Search query expansion at Google relies on grouping equivalent terms, and this screen turns that into a union-find exercise over strings. The interviewer is checking that you use a hash-backed parent map, normalize keys before inserting them, and count roots correctly.
What the Interviewer Expects
- Uses a dictionary-backed union-find for string keys
- Normalizes case before any lookup
- Registers vocabulary words that never appear in a pair
- Counts roots as parent[w] == w
- Discusses complexity in terms of distinct words
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the groups themselves, sorted, for a debugging UI.
- What if some pairs are antonyms that must stay apart?
- How would you shard this when the synonym list has billions of pairs?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of word pairs and a vocabulary list.
- Comparison is case-insensitive, so normalize with
lower(). - Output: number of distinct equivalence classes across all words seen in pairs or voc...
High-Level Design
Keep a dictionary parent that lazily creates a singleton set the first time a word is seen. Register every vocabulary word, then union each synonym pair. The number of meanings is the number of word...