0
0
DSA Goprogramming~10 mins

Autocomplete System with Trie in DSA Go - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new Trie node.

DSA Go
func NewTrieNode() *TrieNode {
    return &TrieNode{
        children: make(map[rune]*TrieNode),
        isEnd:    [1],
    }
}
Drag options to blanks, or click blank then click option'
A0
Btrue
Cnil
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEnd to true for new nodes
Using nil instead of a boolean value
2fill in blank
medium

Complete 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] = NewTrieNode()
        }
        node = node.children[[1]]
    }
    node.isEnd = true
}
Drag options to blanks, or click blank then click option'
Ach
Bword
Cnode
Dt
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole word instead of the character
Using the node variable instead of the character
3fill in blank
hard

Fix the error in the search function to check if a word exists 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'
Achildren
Broot
CisEnd
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Returning children instead of isEnd
Returning root or word which are not boolean flags
4fill in blank
hard

Fill both blanks to collect all words starting with a prefix using DFS.

DSA Go
func (t *Trie) collectWords(node *TrieNode, prefix string, results *[]string) {
    if node.[1] {
        *results = append(*results, prefix)
    }
    for ch, child := range node.[2] {
        t.collectWords(child, prefix+string(ch), results)
    }
}
Drag options to blanks, or click blank then click option'
AisEnd
Broot
Cchildren
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using root instead of children for traversal
Checking children instead of isEnd for word completion
5fill in blank
hard

Fill all three blanks to implement the Autocomplete function returning all words with a prefix.

DSA Go
func (t *Trie) Autocomplete(prefix string) []string {
    node := t.root
    for _, ch := range prefix {
        if node.children[[1]] == nil {
            return []string{}
        }
        node = node.children[ch]
    }
    results := []string{}
    t.collectWords(node, [2], &results)
    return [3]
}
Drag options to blanks, or click blank then click option'
Ach
Bprefix
Cresults
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable to check children
Passing wrong string to collectWords
Returning node instead of results