Longest Handle Buildable One Letter at a Time From the List
Last updated: November 29, 2026
Quick Overview
Given a list of handles, return the longest handle such that every prefix of it (dropping one letter from the end at a time) is also in the list. Break ties by choosing the lexicographically smallest, and return an empty string if none qualifies.
Meta
November 29, 20260
0
0 solved
Given a list of handles, return the longest handle such that every prefix of it (dropping one letter from the end at a time) is also in the list. Break ties by choosing the lexicographically smallest, and return an empty string if none qualifies.
A light trie or set question that Meta uses as a phone-screen warm-up. The interviewer checks that you see sorting makes prefixes arrive first, and that you handle the tie-break correctly.
What the Interviewer Expects
- Notice every prefix must exist, starting from length one
- Sort so shorter prefixes are processed first
- Use a set of buildable words
- Apply the length then lexicographic tie-break
- Mention the trie BFS 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 a trie with BFS solve this without sorting?
- What if one missing prefix is allowed?
- How would you return all handles of the maximum length?
- What changes if letters can be added at the front too?
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 lowercase strings. Output: a single string.
- A handle qualifies if
h[:1],h[:2], ...,hare all in the list (single-letter handles qualify trivially). - Prefer longer; among eq...
High-Level Design
Sort the handles. Lexicographic order places every prefix before its extensions, so when we reach h, we already know whether h[:-1] was buildable. Keep a set built seeded with "". For each han...