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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
trie
string parsing
prefix matching
tokenization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported