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 from root to leaves represent words.
Click to reveal answer
beginner
Why can't a Hash Map efficiently handle prefix-based queries on strings?
Hash Maps do not store keys in any order and do not support prefix search efficiently because they require exact key matches. They cannot quickly find all keys starting with a given prefix.
Click to reveal answer
intermediate
How does a Trie improve prefix search compared to a Hash Map?
A Trie stores strings character by character in a tree structure, allowing quick traversal to all words sharing a prefix by following the prefix path, making prefix search efficient.
Click to reveal answer
intermediate
What is a common use case where Trie is preferred over Hash Map?
Tries are preferred for autocomplete systems, spell checkers, and prefix-based searches where quick retrieval of all words with a given prefix is needed.
Click to reveal answer
intermediate
What is a limitation of Hash Maps when storing large sets of strings?
Hash Maps can consume more memory due to storing full keys and do not support ordered traversal or prefix queries, making them less efficient for certain string operations.
Click to reveal answer
What is the main advantage of a Trie over a Hash Map for string storage?
AFaster exact key lookup
BSupports numeric keys better
CUses less memory
DEfficient prefix search
✗ Incorrect
Tries allow efficient prefix search by storing strings character by character, unlike Hash Maps which require exact key matches.
Hash Maps require exact key matches and do not maintain any order, so prefix searches are not efficient.
Which data structure is best for autocomplete features?
ATrie
BStack
CLinked List
DHash Map
✗ Incorrect
Tries efficiently support prefix queries, making them ideal for autocomplete.
What does each node in a Trie represent?
AA number
BA full word
CA character
DA hash code
✗ Incorrect
Each node in a Trie represents a single character of a string.
Which of these is NOT a limitation of Hash Maps for string keys?
ACannot do prefix search efficiently
BStore keys in a tree structure
CRequire exact key matches
DConsume more memory for large keys
✗ Incorrect
Hash Maps do not store keys in a tree structure; they use hashing.
Explain why Tries are used for prefix-based string searches instead of Hash Maps.
Think about how each data structure stores and accesses keys.
You got /4 concepts.
List the advantages and limitations of Hash Maps when used for storing strings.
Consider lookup speed and search types.
You got /4 concepts.
Practice
(1/5)
1. Why do we use a Trie data structure instead of a Hash Map for storing a large set of strings?
easy
A. Because Trie efficiently supports prefix-based searches which Hash Map cannot do directly
B. Because Trie uses less memory than Hash Map for all data types
C. Because Hash Map cannot store strings as keys
D. Because Trie automatically sorts strings alphabetically
Solution
Step 1: Understand Trie and Hash Map capabilities
Trie stores strings in a tree form where each node represents a character, enabling prefix searches. Hash Map stores key-value pairs but does not support prefix queries efficiently.
Step 2: Compare prefix search support
Trie can quickly find all strings starting with a prefix by traversing nodes. Hash Map requires checking each key individually, which is inefficient.
Final Answer:
Because Trie efficiently supports prefix-based searches which Hash Map cannot do directly -> Option A
Quick Check:
Prefix search = Trie advantage [OK]
Hint: Trie is for prefix search; Hash Map is not [OK]
Common Mistakes:
Thinking Hash Map can do prefix search efficiently
Assuming Trie uses less memory always
Believing Hash Map cannot store strings as keys
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 { List<String> children; boolean isEndOfWord; }
C. class TrieNode { String children; boolean isEndOfWord; }
D. class TrieNode { Map children = new HashMap<>(); boolean isEndOfWord; }
Solution
Step 1: Identify correct data type for children
Trie node children should map characters to next nodes, so Map is correct.
Step 2: Check other options
class TrieNode { int[] children = new int[26]; boolean isEndOfWord; } uses int array which is possible but not a Map; class TrieNode { String children; boolean isEndOfWord; } uses String which is incorrect; class TrieNode { List<String> children; boolean isEndOfWord; } uses List<String> which is not suitable for child nodes.
Final Answer:
class TrieNode { Map children = new HashMap<>(); boolean isEndOfWord; } -> Option D
Quick Check:
Children map = class TrieNode { Map children = new HashMap<>(); boolean isEndOfWord; } [OK]
Hint: Trie children map chars to nodes [OK]
Common Mistakes:
Using String or List instead of Map for children
Using int array without explanation
Forgetting isEndOfWord boolean
3. What will be the output of the following Java code snippet using a Trie?
Trie trie = new Trie();
trie.insert("apple");
trie.insert("app");
System.out.println(trie.startsWith("app"));
System.out.println(trie.search("apple"));
System.out.println(trie.search("apples"));
medium
A. true false true
B. false true false
C. true true false
D. false false false
Solution
Step 1: Understand insert and search operations
Inserting "apple" and "app" adds both words. startsWith("app") checks if any word starts with "app" which is true. search("apple") finds exact word, true. search("apples") not inserted, false.
Hint: startsWith true if prefix exists, search true if exact word [OK]
Common Mistakes:
Confusing startsWith and search results
Assuming search("apples") returns true
Mixing order of outputs
4. The following Java code tries to insert strings into a Trie but throws a NullPointerException. What is the likely cause?
class TrieNode {
Map children;
boolean isEndOfWord;
}
class Trie {
TrieNode root = new TrieNode();
public void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
if (!node.children.containsKey(c)) {
node.children.put(c, new TrieNode());
}
node = node.children.get(c);
}
node.isEndOfWord = true;
}
}
medium
A. children map is not initialized in TrieNode constructor
B. root node is not initialized
C. word.toCharArray() returns null
D. isEndOfWord is not set to false initially
Solution
Step 1: Identify NullPointerException source
NullPointerException likely occurs when calling node.children.containsKey(c) because children map is null.
Step 2: Check initialization of children map
TrieNode class declares children but does not initialize it (e.g., new HashMap<>()), so children is null causing exception.
Final Answer:
children map is not initialized in TrieNode constructor -> Option A
Quick Check:
Uninitialized map = NullPointerException [OK]
Hint: Always initialize children map in TrieNode [OK]
Common Mistakes:
Assuming root is null
Thinking toCharArray() returns null
Ignoring map initialization
5. You want to store a dictionary of 1 million English words and efficiently find all words starting with a given prefix. Why is a Trie better than a Hash Map for this task, and what is a key limitation of Hash Map here?
hard
A. Trie automatically sorts words; Hash Map does not support string keys
B. Trie stores words in a prefix tree allowing fast prefix queries; Hash Map cannot efficiently find keys by prefix without checking all keys
C. Trie uses less memory than Hash Map for large datasets; Hash Map keys must be integers
D. Trie supports deletion of words in O(1); Hash Map deletion is O(n)
Solution
Step 1: Understand prefix search requirement
Finding all words starting with a prefix requires efficient prefix queries, which Trie supports by traversing nodes matching prefix characters.
Step 2: Analyze Hash Map limitation
Hash Map stores keys without order or structure, so to find keys by prefix, it must check all keys, which is slow for large datasets.
Final Answer:
Trie stores words in a prefix tree allowing fast prefix queries; Hash Map cannot efficiently find keys by prefix without checking all keys -> Option B