Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a word into the trie.
DSA Go
func (t *Trie) Insert(word string) {
node := t.root
for _, ch := range word {
if node.children[ch] == nil {
node.children[ch] = &TrieNode{children: make(map[rune]*TrieNode)}
}
node = node.children[[1]]
}
node.isEnd = true
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole word instead of the current character.
Using the node variable incorrectly.
✗ Incorrect
We use the current character 'ch' to move to the child node in the trie during insertion.
2fill in blank
mediumComplete the code to check if a prefix exists in the trie.
DSA Go
func (t *Trie) StartsWith(prefix string) bool {
node := t.root
for _, ch := range prefix {
if node.children[[1]] == nil {
return false
}
node = node.children[ch]
}
return true
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole prefix string instead of the current character.
Checking the wrong variable for children.
✗ Incorrect
We check if the current character 'ch' exists in the children map to verify the prefix.
3fill in blank
hardFix the error in the search function to correctly find a word in the trie.
DSA Go
func (t *Trie) Search(word string) bool {
node := t.root
for _, ch := range word {
if node.children[ch] == nil {
return false
}
node = node.children[ch]
}
return node.[1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the children map instead of the end flag.
Returning the root or word variable incorrectly.
✗ Incorrect
To confirm the word exists, we check if the last node marks the end of a word using 'isEnd'.
4fill in blank
hardFill both blanks to implement a function that returns all words with a given prefix.
DSA Go
func (t *Trie) WordsWithPrefix(prefix string) []string {
node := t.root
for _, ch := range prefix {
if node.children[ch] == nil {
return nil
}
node = node.children[ch]
}
var results []string
var dfs func(n *TrieNode, path string)
dfs = func(n *TrieNode, path string) {
if n.[1] {
results = append(results, path)
}
for c, child := range n.[2] {
dfs(child, path+string(c))
}
}
dfs(node, prefix)
return results
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names like 'root' or 'word' instead of 'children' or 'isEnd'.
Not appending words when 'isEnd' is true.
✗ Incorrect
We check 'isEnd' to add words to results and iterate over 'children' to continue DFS.
5fill in blank
hardFill all three blanks to implement a function that deletes a word from the trie.
DSA Go
func (t *Trie) Delete(word string) bool {
var deleteHelper func(node *TrieNode, word string, depth int) bool
deleteHelper = func(node *TrieNode, word string, depth int) bool {
if node == nil {
return false
}
if depth == len(word) {
if !node.[1] {
return false
}
node.[2] = false
return len(node.[3]) == 0
}
ch := rune(word[depth])
if deleteHelper(node.children[ch], word, depth+1) {
delete(node.children, ch)
return !node.[1] && len(node.children) == 0
}
return false
}
return deleteHelper(t.root, word, 0)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'root' with 'children' or 'isEnd'.
Not unsetting the 'isEnd' flag when deleting.
✗ Incorrect
We check and unset 'isEnd' to mark deletion, and check 'children' map length to remove nodes.