EvalFrame Technical Mock Interviews & Coding Practice

Implement Trie (Prefix Tree)

Medium · Trie · Algorithm problem · all problems

A **trie** (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. Implement the `Trie` class: * `insert(word: string): void` Inserts the string `word` into the trie. * `search(word: string): boolean` Returns `true` if the string `word` is in the trie, and `false` otherwise. * `startsWith(prefix: string): boolean` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`. **Examples:** ```typescript const trie = new Trie(); trie.insert("apple"); trie.search("apple"); // return True trie.search("app"); // return False trie.startsWith("app"); // return True trie.insert("app"); trie.search("app"); // return True ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(m) per operation
  • Space complexity: O(total characters inserted)

Insert, search, and startsWith all run in time proportional to word length m.

Follow-Up Questions Interviewers Ask About Implement Trie (Prefix Tree)

  • How does a Trie compare to a Hash Table for string lookups?
    Tries support prefix queries like startsWith and auto-complete in O(prefix_length), whereas hash tables cannot find prefixes without scanning all keys.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.