Shorten Every Caption Word to Its Shortest Known Root
Last updated: November 29, 2026
Quick Overview
Given a list of root words and a caption, replace each word in the caption with the shortest root that is a prefix of it, leaving words with no matching root unchanged. Return the rewritten caption.
Meta
November 29, 20260
0
0 solved
Given a list of root words and a caption, replace each word in the caption with the shortest root that is a prefix of it, leaving words with no matching root unchanged. Return the rewritten caption.
Meta's text understanding pipelines normalize words before counting or matching them, and stemming against a root dictionary is a simple instance. The interviewer is checking that you use a trie to stop at the first root rather than testing every root against every word.
What the Interviewer Expects
- Insert roots into a trie with end markers
- Walk each word and stop at the first end marker
- Fall back to the original word when the walk breaks
- Avoid quadratic string slicing
- Compare against the set-of-roots alternative
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 choose the longest root instead?
- How would you handle uppercase and punctuation attached to words?
- What if roots can also be suffixes?
- How would you process a caption stream too large for memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Roots and caption words are lowercase letters; the caption is words separated by single spaces.
- For each word, use the shortest root that is a prefix; if a root equals the word, the word stays...
High-Level Design
Insert every root into a trie and mark terminal nodes. For each caption word, walk the trie character by character. The first time we land on a terminal node, the prefix so far is the shortest root, s...