Implement a Trie with Deletion

Data Structures
Hard
Microsoft
31.7K views

Implement the Trie data structure, including a robust `delete` operation that removes a key while maintaining the integrity of other keys that share prefixes.

Why Interviewers Ask This

Microsoft asks this to evaluate a candidate's mastery of pointer manipulation and edge case handling within complex data structures. The deletion operation specifically tests whether you can maintain structural integrity while removing nodes without breaking shared prefixes, revealing your ability to write production-ready code that handles subtle memory management scenarios.

How to Answer This Question

1. Clarify requirements by confirming if the key must be fully removed or just marked as deleted, and how to handle nodes with zero children. 2. Define the TrieNode structure explicitly, ensuring it includes a boolean flag for end-of-word and an array or map for children pointers. 3. Propose a recursive solution for deletion, explaining that you will traverse down to the target character, then backtrack to update parent references. 4. Detail the critical logic: only remove a node if it has no children and is not an end-of-word marker; otherwise, preserve it to protect other keys sharing the prefix. 5. Walk through a concrete example, such as deleting 'apple' from a trie containing 'app', to demonstrate how the algorithm preserves the 'p' nodes while removing 'l', 'e', and the final leaf. 6. Analyze time complexity (O(M) where M is key length) and space complexity (O(1) auxiliary space excluding recursion stack).

Key Points to Cover

  • Explicitly handling the distinction between removing a key and physically freeing the node memory
  • Correctly identifying when a node can be safely removed versus when it must be preserved for shared prefixes
  • Using a recursive backtracking strategy to clean up the tree structure efficiently
  • Verifying the existence of the key before attempting any deletion operations
  • Maintaining O(L) time complexity relative to the key length

Sample Answer

To implement a Trie with deletion, I first define a Node class containing a map of children and a boolean flag indicating if a word ends here. The core challenge lies in the delete function, which requires careful backtracking. My approach uses recursion. First, I check if the current node exists; if not, the key isn't there. If we reach the end of the key, I verify the end-of-word flag. If false, the key doesn't exist. If true, I set the flag to false. The crucial step happens during the return phase. After recursively calling delete on the child node, I check if that child node is now empty. A node is considered empty if it has no children AND its end-of-word flag is false. If both conditions are met, I remove the child reference from the parent. This ensures we don't orphan nodes needed for other words like 'app' when deleting 'apple'. For example, deleting 'car' from a trie containing 'cart' and 'card' would require checking children after removal. Since 'c' and 'a' share prefixes with other words, they remain. Only the specific path for 'r' might be pruned if it becomes isolated. This method guarantees O(L) time complexity, where L is the string length, and maintains the Trie's efficiency for lookups and insertions.

Common Mistakes to Avoid

  • Deleting a node immediately upon finding the key without checking if it serves as a prefix for other existing words
  • Failing to update the end-of-word flag before attempting to prune child nodes
  • Implementing an iterative solution that incorrectly manages parent pointers during traversal
  • Ignoring the scenario where the root node itself needs to be returned to null if the trie becomes empty

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 65 Microsoft questions