Generate Every Caption Variant From Synonym Pairs
Last updated: November 29, 2026
Quick Overview
Given pairs of synonymous words (synonymy is transitive) and a caption, return every caption that can be formed by replacing each word with any of its synonyms, sorted lexicographically.
Meta
November 29, 20260
0
0 solved
Given pairs of synonymous words (synonymy is transitive) and a caption, return every caption that can be formed by replacing each word with any of its synonyms, sorted lexicographically.
A Meta technical screen problem with an integrity and ML data angle, such as expanding a caption into paraphrase variants to test a text classifier's robustness. The interviewer checks union find over words to build synonym classes, followed by a clean cartesian product, and awareness that output size can blow up.
What the Interviewer Expects
- Union words that appear in synonym pairs
- Group words by root and sort each group
- Take the cartesian product over caption positions
- Leave words without synonyms unchanged
- Discuss exponential output size
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
- How would you cap the output to the k most likely variants?
- What if synonyms are not transitive (bank/river and bank/money)?
- How would you use these variants to measure classifier robustness?
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:
pairsof words andcaption(space-separated words). - Synonymy is an equivalence relation built from the pairs.
- Output: all variant captions, sorted....
High-Level Design
- Classes. Union find keyed by word strings. Every pair merges two classes.
- Choices per position. For each caption word, its options are the sorted class members if it appears in any pair,...