Longest Codename Buildable One Letter at a Time

Last updated: November 29, 2026

Quick Overview

Given a list of words, find the longest word such that every one of its prefixes is also in the list; break ties by choosing the lexicographically smallest.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a list of words, find the longest word such that every one of its prefixes is also in the list; break ties by choosing the lexicographically smallest.

A Google online assessment style problem phrased around release codenames that grow one letter per version. It checks that you can use a trie (or a sorted set) to confirm all prefixes exist and get the tie-breaking rule right.

What the Interviewer Expects
  • Insert all words into a trie with end markers
  • Traverse only through nodes that are word ends
  • Track the longest word found, smallest on ties
  • Compare with a sort plus hash set alternative
  • Handle the empty result case
Key Topics to Cover
trie
BFS
DFS
lexicographic tie-break
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
  • What if a word may be built by adding a letter at either end?
  • How would you return all words of maximum length?
  • How does the answer change if one missing prefix is allowed?
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

Input: words, lowercase strings. Output: the longest word w where w[:1], w[:2], ..., w are all in words. On ties, return the lexicographically smallest. Return an empty string if no single...

High-Level Design

Insert every word into a trie, marking ends. A word qualifies exactly when every node on its path is marked. So do a traversal from the root that only steps into children whose end flag is set. Every ...


Submit Your Answer
Markdown supported