0
0
DSA Goprogramming~20 mins

Word Search in Trie in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Word Search in Trie after Insertions
What is the output of the following Go code that inserts words into a Trie and searches for a specific word?
DSA Go
package main

import "fmt"

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

type Trie struct {
    root *TrieNode
}

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

func NewTrie() *Trie {
    return &Trie{root: NewTrieNode()}
}

func (t *Trie) Insert(word string) {
    node := t.root
    for _, ch := range word {
        if _, ok := node.children[ch]; !ok {
            node.children[ch] = NewTrieNode()
        }
        node = node.children[ch]
    }
    node.isEnd = true
}

func (t *Trie) Search(word string) bool {
    node := t.root
    for _, ch := range word {
        if _, ok := node.children[ch]; !ok {
            return false
        }
        node = node.children[ch]
    }
    return node.isEnd
}

func main() {
    trie := NewTrie()
    trie.Insert("go")
    trie.Insert("gone")
    trie.Insert("gopher")
    fmt.Println(trie.Search("go"))
    fmt.Println(trie.Search("gone"))
    fmt.Println(trie.Search("goph"))
}
A
true
true
true
B
true
true
false
C
false
true
false
D
true
false
false
Attempts:
2 left
💡 Hint
Remember that Search returns true only if the word is fully present and marked as an end.
🧠 Conceptual
intermediate
1:30remaining
Understanding Trie Node Structure
Which of the following best describes the role of the 'isEnd' boolean in a Trie node?
AIt marks the node as the end of a valid inserted word.
BIt counts how many words pass through this node.
CIt stores the character value of the node.
DIt indicates if the node has any children nodes.
Attempts:
2 left
💡 Hint
Think about how the Trie distinguishes between prefixes and complete words.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Trie Search Method
What error will the following Go code produce when searching for a word not in the Trie?
DSA Go
func (t *Trie) Search(word string) bool {
    node := t.root
    for _, ch := range word {
        node = node.children[ch]
    }
    return node.isEnd
}
Acompilation error: missing if check
Breturns false correctly
Cruntime error: invalid memory address or nil pointer dereference
Dreturns true incorrectly
Attempts:
2 left
💡 Hint
What happens if the character is not found in children map?
🚀 Application
advanced
1:00remaining
Number of Words Stored in Trie
Given a Trie with the words inserted: ["cat", "car", "cart", "dog", "dot"], how many words does the Trie contain?
A5
B6
C3
D4
Attempts:
2 left
💡 Hint
Count each unique complete word inserted.
Predict Output
expert
2:30remaining
Output of Trie Search with Prefixes and Words
What is the output of this Go program that inserts words and searches for prefixes and words?
DSA Go
package main

import "fmt"

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

type Trie struct {
    root *TrieNode
}

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

func NewTrie() *Trie {
    return &Trie{root: NewTrieNode()}
}

func (t *Trie) Insert(word string) {
    node := t.root
    for _, ch := range word {
        if _, ok := node.children[ch]; !ok {
            node.children[ch] = NewTrieNode()
        }
        node = node.children[ch]
    }
    node.isEnd = true
}

func (t *Trie) Search(word string) bool {
    node := t.root
    for _, ch := range word {
        if _, ok := node.children[ch]; !ok {
            return false
        }
        node = node.children[ch]
    }
    return node.isEnd
}

func main() {
    trie := NewTrie()
    trie.Insert("apple")
    trie.Insert("app")
    trie.Insert("application")
    fmt.Println(trie.Search("app"))
    fmt.Println(trie.Search("apple"))
    fmt.Println(trie.Search("appl"))
    fmt.Println(trie.Search("application"))
}
A
false
true
false
true
B
true
false
false
true
C
true
true
true
true
D
true
true
false
true
Attempts:
2 left
💡 Hint
Only words fully inserted and marked as end return true.