As reported: Citadel phone screen (June 2025): a trie over lowercase letters, a follow-up on its memory use, and a bonus query.
Implement a class Trie over lowercase English letters:
insert(word)— storeword.search(word)—trueifwordwas inserted.startsWith(prefix)—trueif some inserted word starts withprefix.
These three are LeetCode 208 — Implement Trie (Prefix Tree).
Bonus. longestPrefixWhichStartsWithPrefix(prefix) — among the stored words that start with prefix, return their longest common prefix: how far all of them agree beyond prefix. Return "" if no stored word starts with prefix.
Example
insert("abcdef")
insert("abcdeg")
longestPrefixWhichStartsWithPrefix("abc") -> "abcde"
search("abcde") -> false
startsWith("abcde") -> true
insert("abcd")
longestPrefixWhichStartsWithPrefix("abc") -> "abcd"
longestPrefixWhichStartsWithPrefix("x") -> ""
Follow-ups
- A node with a 26-slot child array wastes space. How would you store the children instead?
Constraints: at most 3 · 10^4 calls; words and prefixes have 1–2000 lowercase letters; total length of inserted words ≤ 10^6.
Code runner format: the first line is the number of calls q; each of the next q lines is insert w, search w, startsWith p or longest p (for longestPrefixWhichStartsWithPrefix). The harness prints one line per call except insert: true / false, or the returned string (- for "").