The Implement Trie II coding problem is an extension of the standard Trie data structure. In addition to the basic insert, search, and startsWith methods, you need to track frequencies. Specifically, you must implement:
countWordsEqualTo(word): Return how many times a word has been inserted.countWordsStartingWith(prefix): Return how many words have the given prefix.erase(word): Remove one instance of a word from the Trie.Companies like Microsoft and DoorDash use this to test a candidate's ability to maintain state within a tree. It moves beyond simple existence checks to counting and deletion, which requires updating metadata at every node along a path. It evaluates your understanding of how to propagate changes through a hierarchical data structure.
This follows the Trie interview pattern with Node Metadata.
count: Number of words that end at this node.prefixCount: Number of words that pass through this node.prefixCount for every node on the path. Increment count at the final node.prefixCount for every node on the path. Decrement count at the final node.prefixCount = 1. The 'e' node gets count = 1.prefixCount = 2, 'e' gets count = 2.count (2).prefixCount = 1, 'e' gets count = 1.prefixCount (1).prefixCount (though simple decrementing is often enough for interview logic).Trie II is about Metadata Management. Whenever you insert or delete, think about the "trail" you leave behind. Every node on the path must be aware of the total number of words it supports.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design File System | Medium | Solve | |
| Implement Trie (Prefix Tree) | Medium | Solve | |
| Map Sum Pairs | Medium | Solve | |
| Implement Magic Dictionary | Medium | Solve | |
| Prefix and Suffix Search | Hard | Solve |