0
0
DSA Goprogramming~10 mins

Longest Word in Dictionary Using 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
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'
Atrue
Bnil
Cfalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting isEnd to true initially, which incorrectly marks the node as end of a word.
2fill in blank
medium

Complete 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'
Ach
Bindex
Cnode
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character directly instead of the index to access children.
Using the word or node variable incorrectly.
3fill in blank
hard

Fix 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'
Aresult
Bnode
Cpath
Di
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.
4fill in blank
hard

Fill 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'
AInsert
BSearch
Cresult
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using Search instead of Insert to add words.
Returning the wrong variable.
5fill in blank
hard

Fill 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'
Ai
Bj
Ck
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts causing syntax errors.