Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new Trie node.
DSA Go
type TrieNode struct {
children [26]*TrieNode
isEnd bool
}
func NewTrieNode() *TrieNode {
return &TrieNode{
children: [26]*TrieNode{},
isEnd: [1],
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEnd to true initially, which incorrectly marks the node as end of a word.
✗ Incorrect
When creating a new Trie node, the isEnd flag should be false initially because no word ends at this node yet.
2fill in blank
mediumComplete the code to insert a word into the Trie.
DSA Go
func (t *TrieNode) Insert(word string) {
node := t
for _, ch := range word {
index := ch - 'a'
if node.children[index] == nil {
node.children[index] = NewTrieNode()
}
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 character directly instead of the index to access children.
Using the word or node variable incorrectly.
✗ Incorrect
We use the calculated index to move to the correct child node for the current character.
3fill in blank
hardFix the error in the DFS function to find the longest word.
DSA Go
func dfs(node *TrieNode, path string, result *string) {
if !node.isEnd && len(path) > 0 {
return
}
if len(path) > len(*result) || (len(path) == len(*result) && path < *result) {
*result = path
}
for i := 0; i < 26; i++ {
if node.children[i] != nil {
dfs(node.children[i], path+string('a'+[1]), result)
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using node or path instead of the loop index i to calculate the character.
Appending wrong characters causing incorrect word formation.
✗ Incorrect
We add the character corresponding to index i to the path during DFS traversal.
4fill in blank
hardFill both blanks to complete the function that finds the longest word in the dictionary using Trie.
DSA Go
func longestWord(words []string) string {
root := NewTrieNode()
for _, word := range words {
root.[1](word)
}
result := ""
dfs(root, "", &result)
return [2]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Search instead of Insert to add words.
Returning the wrong variable.
✗ Incorrect
We insert each word into the Trie, then return the result found by DFS.
5fill in blank
hardFill all three blanks to complete the DFS helper function that builds the longest word.
DSA Go
func dfs(node *TrieNode, path string, result *string) {
if !node.isEnd && len(path) > 0 {
return
}
if len(path) > len(*result) || (len(path) == len(*result) && path < *result) {
*result = path
}
for [1] := 0; [2] < 26; [3]++ {
if node.children[[1]] != nil {
dfs(node.children[[1]], path+string('a'+[1]), result)
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts causing syntax errors.
✗ Incorrect
The loop variable i is used consistently for initialization, condition, and increment.