Find the Longest Search Term Buildable One Letter at a Time
Last updated: November 29, 2026
Quick Overview
Given a dictionary of words, return the longest word that can be built one character at a time where every prefix is also in the dictionary. Break ties by the lexicographically smallest word.
Amazon
November 29, 20260
0
0 solved
Given a dictionary of words, return the longest word that can be built one character at a time where every prefix is also in the dictionary. Break ties by the lexicographically smallest word.
An online assessment style question in Amazon's style, framed around a search team checking that typeahead coverage exists for every prefix of a popular query. It tests whether you see that a trie with end-of-word flags answers the prefix condition directly.
What the Interviewer Expects
- Recognizes every prefix must be a word, not just a prefix
- Builds a trie with end markers
- Traverses only through end-marked nodes
- Applies the tie-breaker correctly
- Offers the simpler sort plus set approach too
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 return all words of maximum length?
- How would you handle a stream of new words while keeping the answer current?
- What changes if prefixes may skip one letter?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Return the longest word whose every prefix (length 1 to len-1) is itself a word in the list. Ties go to the lexicographically smallest. If none qualify, return the empty string....
High-Level Design
Insert every word into a trie and store the word at its terminal node. A word qualifies exactly when the path from the root to it passes only through terminal nodes. So run a DFS from the root that on...