A Trie is a tree-like data structure that stores a dynamic set of strings, where each node represents a character. It is used to efficiently search for words or prefixes.
Click to reveal answer
beginner
How does a Trie help in prefix search?
A Trie allows prefix search by following the path of characters from the root node down to the node representing the last character of the prefix. All words under this node share the prefix.
Click to reveal answer
intermediate
What is the time complexity of searching a prefix in a Trie?
The time complexity is O(m), where m is the length of the prefix. This is because we traverse one node per character in the prefix.
Click to reveal answer
beginner
In a Trie node, what does the 'children' map represent?
The 'children' map stores links to child nodes, each corresponding to a character that can follow the current node's character in stored words.
Click to reveal answer
beginner
What does it mean when a Trie node is marked as 'end of word'?
It means that the path from the root to this node forms a complete word stored in the Trie.
Click to reveal answer
What is the main advantage of using a Trie for prefix search?
AIt allows searching prefixes in time proportional to prefix length
BIt uses less memory than arrays
CIt sorts words automatically
DIt stores only unique words
✗ Incorrect
A Trie lets you find prefixes by following nodes for each character, so search time depends only on prefix length.
In a Trie, what does each node typically represent?
AA number
BA whole word
CA character
DA prefix length
✗ Incorrect
Each node in a Trie represents a single character in the stored strings.
If a prefix is not found in a Trie, what does that mean?
AThe Trie is empty
BNo word in the Trie starts with that prefix
CAll words have that prefix
DThe prefix is a complete word
✗ Incorrect
If the prefix path does not exist, no stored word starts with that prefix.
What data structure is commonly used inside a Trie node to store children?
AMap or dictionary
BStack
CQueue
DLinked list
✗ Incorrect
A map or dictionary is used to link characters to child nodes efficiently.
What is the space complexity concern with Tries?
AThey compress all words
BThey use less memory than hash tables
CThey store only one word
DThey can use a lot of memory due to many nodes
✗ Incorrect
Tries can consume a lot of memory because each character creates a node, especially with many words.
Explain how prefix search works using a Trie data structure.
Think about walking down the tree one character at a time.
You got /4 concepts.
Describe the structure of a Trie node and its role in prefix search.
Imagine each node as a letter box with links to next letters.
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 sort words alphabetically
B. To efficiently find all words that start with a given prefix
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 begin with a certain starting sequence of letters.
Step 2: Understand Trie's role in prefix search
A Trie stores words in a tree-like structure where each node represents a character, making it fast to find all words sharing a prefix.
Final Answer:
To efficiently find all words that start with a given prefix -> Option B
Quick Check:
Prefix search = efficient word lookup [OK]
Hint: Trie nodes represent characters 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 struct in Go for prefix search?
easy
A. type TrieNode struct { value int; left *TrieNode; right *TrieNode }
B. type TrieNode struct { children []int; isEnd int }
C. type TrieNode struct { data string; next *TrieNode }
D. type TrieNode struct { children map[rune]*TrieNode; isEnd bool }
Solution
Step 1: Identify the Trie node components
A Trie node should have a map from characters (rune) to child nodes and a boolean to mark word end.
Step 2: Match the correct Go struct syntax
type TrieNode struct { children map[rune]*TrieNode; isEnd bool } uses a map[rune]*TrieNode and a bool isEnd, which fits the Trie node definition.
Final Answer:
type TrieNode struct { children map[rune]*TrieNode; isEnd bool } -> Option D
Quick Check:
Trie node = map + bool [OK]
Hint: Trie nodes use map[rune]*TrieNode and bool isEnd [OK]
Common Mistakes:
Using slice of int instead of map for children
Confusing linked list node with Trie node
Using int instead of bool for isEnd
3. Given the following Go code snippet for inserting words into a Trie and searching for prefix "app":
trie := NewTrie()
trie.Insert("apple")
trie.Insert("app")
trie.Insert("application")
result := trie.StartsWith("app")
fmt.Println(result)
What will be the output of fmt.Println(result)?
medium
A. [app]
B. [apple application]
C. [apple app application]
D. []
Solution
Step 1: Insert words into the Trie
Words "apple", "app", and "application" are inserted, all starting with prefix "app".
Step 2: Search for prefix "app"
The StartsWith method returns all words starting with "app", which includes all three inserted words.
Final Answer:
[apple app application] -> Option C
Quick Check:
All words with prefix "app" = [apple app application] [OK]
Hint: StartsWith returns all words sharing the prefix [OK]
Common Mistakes:
Missing the exact prefix word "app" in results
Returning only one matching word
Returning empty list if prefix matches
4. Consider this buggy Go code snippet for searching a prefix in a Trie:
What is the main bug causing incorrect prefix search results?
medium
A. The code does not collect all words under the prefix node
B. The code does not check if node.words is initialized
C. The code returns nil instead of empty slice when prefix not found
D. The code uses wrong variable name for children map
Solution
Step 1: Analyze the prefix traversal loop
The loop correctly moves node down the Trie for each character in prefix.
Step 2: Check the return statement after traversal
Returning node.words assumes all words under prefix are stored there, but usually words are stored in leaf nodes, so it misses collecting all words under subtree.
Final Answer:
The code does not collect all words under the prefix node -> Option A
Quick Check:
Prefix search must gather all subtree words [OK]
Hint: Collect all subtree words after prefix node for correct results [OK]
Common Mistakes:
Returning nil instead of empty slice is minor, not main bug
Assuming words stored only at prefix node
Ignoring recursive collection of words
5. You want to implement a prefix search that returns only words starting with a prefix but excludes words that are exactly the prefix itself. Which modification to the Trie search logic achieves this?
hard
A. After finding prefix node, return all words except the one equal to prefix
B. Return only words that have prefix length or fewer characters
C. Skip marking isEnd for prefix nodes during insertion
D. Return all words including prefix itself
Solution
Step 1: Understand the requirement
We want all words starting with prefix but exclude the word exactly equal to prefix.
Step 2: Modify search result filtering
After collecting all words under prefix node, filter out the word that matches prefix exactly before returning.
Final Answer:
After finding prefix node, return all words except the one equal to prefix -> Option A
Quick Check:
Filter out exact prefix word from results [OK]
Hint: Filter out exact prefix word after collecting all matches [OK]
Common Mistakes:
Removing isEnd flag breaks normal word insertion
Returning only longer words by length may miss valid words
Returning prefix itself defeats the requirement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.