Question Bank
/

Trie Essentials

Trie Essentials

Prompts on the prefix-tree shape, insert and lookup costs, and the autocomplete pattern. Builds the model before tackling KMP-like or word-search problems.

Question Bank
Medium
Python
4 questions
trie
autocomplete
data-structures
interview-prep

432 views

6

Implement a Trie class with insert(word) and search(word) returning whether the exact word was inserted. Use a dictionary at each node.

Examples

Example 1:

Input: insert('cat'), insert('car'); then search('cat'), search('ca'), search('cars')
Output: True, False, False
Explanation: Each node has a children dict plus an is_end flag. search returns is_end at the destination; 'ca' lands on an internal node so is_end is False, and 'cars' falls off the trie at 's'.
class Trie:
    def __init__(self):
        # TODO
        pass
    def insert(self, word):
        # TODO
        pass
    def search(self, word):
        # TODO
        pass