A Trie is a tree-like data structure used to store a dynamic set of strings where keys are usually strings. Each node represents a character, and paths down the tree represent words or prefixes.
Click to reveal answer
beginner
How does a Trie help in prefix search?
A Trie allows quick prefix search by traversing nodes corresponding to each character of the prefix. If traversal succeeds, all words under that node share the prefix.
Click to reveal answer
beginner
What is stored in each node of a Trie?
Each node stores links to child nodes for possible characters and a boolean flag indicating if the node marks the end of a valid word.
Click to reveal answer
intermediate
Describe the steps to insert a word into a Trie.
Start at the root. For each character in the word, check if a child node exists. If not, create it. Move to the child node. After the last character, mark the node as end of word.
Click to reveal answer
intermediate
How to retrieve all words with a given prefix in a Trie?
Traverse the Trie nodes for the prefix. From the last prefix node, perform a depth-first search to collect all words that have that prefix.
Click to reveal answer
What does each node in a Trie typically represent?
AA character
BA whole word
CAn integer value
DA prefix length
✗ Incorrect
Each node in a Trie represents a single character of a word or prefix.
What is the time complexity to search a prefix of length k in a Trie?
AO(log n)
BO(n)
CO(k)
DO(k^2)
✗ Incorrect
Searching a prefix of length k requires traversing k nodes, so time complexity is O(k).
Which of these is NOT a typical use case of a Trie?
ASorting numbers
BSpell checking
CPrefix search
DAutocomplete
✗ Incorrect
Tries are not used for sorting numbers; they are mainly for string prefix operations.
What flag is used in Trie nodes to indicate a complete word?
AisPrefix
BisWordEnd
CisRoot
DisLeaf
✗ Incorrect
The isWordEnd flag marks that the path to this node forms a complete word.
To find all words with a prefix, what traversal is used after reaching the prefix node?
ALinear search
BBreadth-first search
CBinary search
DDepth-first search
✗ Incorrect
Depth-first search is used to explore all child nodes to collect words with the prefix.
Explain how to insert a word into a Trie and how it helps in prefix search.
Think about how each character leads to a node and how prefixes share nodes.
You got /4 concepts.
Describe the process to find all words starting with a given prefix using a Trie.
Focus on the two-step process: locating prefix and exploring descendants.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a Trie data structure in prefix search?
easy
A. To efficiently find all words that start with a given prefix
B. To sort words alphabetically
C. To store numbers in sorted order
D. To perform arithmetic operations quickly
Solution
Step 1: Understand what prefix search means
Prefix search means finding all words that start with a certain beginning sequence of letters.
Step 2: Understand Trie's role in prefix search
A Trie stores words in a tree where each node represents a letter, making it easy to find all words sharing the same prefix by traversing the tree.
Final Answer:
To efficiently find all words that start with a given prefix -> Option A
Quick Check:
Prefix search = efficient word lookup [OK]
Hint: Trie nodes represent letters for fast prefix lookup [OK]
Common Mistakes:
Confusing Trie with sorting data structures
Thinking Trie is for arithmetic operations
Assuming Trie stores numbers only
2. Which of the following is the correct way to declare a Trie node class in Java?
easy
A. class TrieNode { int[] children = new int[26]; boolean isEndOfWord; }
B. class TrieNode { char[] children = new char[26]; boolean isEndOfWord; }
C. class TrieNode { String[] children = new String[26]; boolean isEndOfWord; }
D. class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord; }
Solution
Step 1: Identify the correct type for children nodes
Each child should be a reference to another TrieNode, so the array type must be TrieNode[].
Step 2: Check the other parts of the class
Boolean isEndOfWord marks if the node ends a word, which is correct in class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord; }.
Final Answer:
class TrieNode { TrieNode[] children = new TrieNode[26]; boolean isEndOfWord; } -> Option D
Quick Check:
Children are TrieNode references [OK]
Hint: Children array holds TrieNode references, not primitives [OK]
Common Mistakes:
Using int or char arrays instead of TrieNode array
Using String array for children
Forgetting to mark end of word
3. Given the following code snippet, what will be the output?
Words "cat", "car", and "dog" are inserted, so prefixes "ca" and "do" exist in the Trie.
Step 2: Check startsWith results
startsWith("ca") returns true because "cat" and "car" start with "ca". startsWith("do") returns true because "dog" starts with "do". startsWith("bat") returns false because no word starts with "bat".