0
0
DSA Goprogramming~20 mins

Trie Insert Operation in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after inserting words into a Trie
What is the printed state of the Trie after inserting the words "cat", "car", and "dog" in order?
DSA Go
package main

import "fmt"

type TrieNode struct {
    children map[rune]*TrieNode
    isEnd    bool
}

type Trie struct {
    root *TrieNode
}

func NewTrie() *Trie {
    return &Trie{root: &TrieNode{children: make(map[rune]*TrieNode)}}
}

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[ch]
    }
    node.isEnd = true
}

func (t *Trie) Print(node *TrieNode, prefix string) {
    if node.isEnd {
        fmt.Println(prefix)
    }
    for ch, child := range node.children {
        t.Print(child, prefix+string(ch))
    }
}

func main() {
    trie := NewTrie()
    trie.Insert("cat")
    trie.Insert("car")
    trie.Insert("dog")
    trie.Print(trie.root, "")
}
A
car
cat
dog
B
cat
car
dog
C
dog
car
cat
D
cat
dog
car
Attempts:
2 left
💡 Hint
The Print function prints words in lexicographical order because it iterates over children map keys in Go's map iteration order, which is random. But since map iteration order is random, the output order depends on insertion and map iteration.
🧠 Conceptual
intermediate
1:30remaining
Number of nodes after inserting words in a Trie
After inserting the words "apple", "app", and "ape" into an empty Trie, how many nodes does the Trie contain (including the root node)?
A7
B8
C6
D9
Attempts:
2 left
💡 Hint
Count shared prefixes only once.
🔧 Debug
advanced
2:00remaining
Identify the error in Trie Insert method
What error will this Trie Insert method cause when inserting the word "bat"?
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[ch]
    }
    node.isEnd = true
}
ARuntime panic: assignment to nil map
BNil pointer dereference panic
CNo error, inserts correctly
DInfinite loop
Attempts:
2 left
💡 Hint
Check the condition that creates new nodes.
📝 Syntax
advanced
1:30remaining
Which option correctly defines the TrieNode struct with children map?
Select the correct Go struct definition for a TrieNode that holds children nodes and an end-of-word flag.
A
type TrieNode struct {
    children map[int]*TrieNode
    isEnd bool
}
B
type TrieNode struct {
    children map[string]*TrieNode
    isEnd bool
}
C
type TrieNode struct {
    children []*TrieNode
    isEnd bool
}
D
type TrieNode struct {
    children map[rune]*TrieNode
    isEnd bool
}
Attempts:
2 left
💡 Hint
Children keys should be characters, not strings or ints.
🚀 Application
expert
2:30remaining
Resulting Trie after inserting and searching words
After inserting "tree", "trie", "algo", and "algo" again into an empty Trie, which words will be found by searching for "trie" and "alg"?
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.isEnd
}
A"trie" found: true, "alg" found: true
B"trie" found: false, "alg" found: true
C"trie" found: true, "alg" found: false
D"trie" found: false, "alg" found: false
Attempts:
2 left
💡 Hint
"alg" is a prefix but not a complete inserted word.