0
0
DSA Goprogramming~20 mins

Trie Search Operation 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 Trie Search for Existing Word
What is the output of the following Go code that inserts words into a Trie and searches for "cat"?
DSA Go
package main
import "fmt"

// TrieNode represents a node in the Trie
type TrieNode struct {
    children map[rune]*TrieNode
    isEnd    bool
}

// Trie structure
type Trie struct {
    root *TrieNode
}

// Initialize a new TrieNode
func newNode() *TrieNode {
    return &TrieNode{children: make(map[rune]*TrieNode)}
}

// Insert a word into the Trie
func (t *Trie) Insert(word string) {
    current := t.root
    for _, ch := range word {
        if _, ok := current.children[ch]; !ok {
            current.children[ch] = newNode()
        }
        current = current.children[ch]
    }
    current.isEnd = true
}

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

func main() {
    trie := &Trie{root: newNode()}
    trie.Insert("cat")
    trie.Insert("car")
    trie.Insert("dog")
    fmt.Println(trie.Search("cat"))
}
Apanic: runtime error
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Think about whether the word "cat" was inserted before searching.
Predict Output
intermediate
2:00remaining
Output of Trie Search for Non-Existing Word
What is the output of the following Go code that inserts words into a Trie and searches for "bat"?
DSA Go
package main
import "fmt"

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

type Trie struct {
    root *TrieNode
}

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

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

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

func main() {
    trie := &Trie{root: newNode()}
    trie.Insert("cat")
    trie.Insert("car")
    trie.Insert("dog")
    fmt.Println(trie.Search("bat"))
}
ACompilation error
Btrue
Cfalse
Dpanic: runtime error
Attempts:
2 left
💡 Hint
Check if "bat" was inserted before searching.
🧠 Conceptual
advanced
1:30remaining
Understanding Trie Search Behavior
In a Trie, what does it mean if the search for a word returns true?
AThe Trie is empty.
BThe word exists in the Trie and ends at a node marked as end of word.
CThe word does not exist but shares some characters with words in the Trie.
DThe word is a prefix of some word in the Trie but not necessarily a complete word.
Attempts:
2 left
💡 Hint
Think about the isEnd flag in Trie nodes.
Predict Output
advanced
2:00remaining
Output of Trie Search with Partial Word
What is the output of the following Go code that inserts words into a Trie and searches for "ca"?
DSA Go
package main
import "fmt"

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

type Trie struct {
    root *TrieNode
}

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

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

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

func main() {
    trie := &Trie{root: newNode()}
    trie.Insert("cat")
    trie.Insert("car")
    trie.Insert("dog")
    fmt.Println(trie.Search("ca"))
}
Afalse
Btrue
CCompilation error
Dpanic: runtime error
Attempts:
2 left
💡 Hint
Is "ca" marked as a complete word in the Trie?
🧠 Conceptual
expert
2:30remaining
Why is the isEnd Flag Important in Trie Search?
Why do Trie nodes have an isEnd flag, and what would happen if it was missing during search?
AIt marks the end of a complete word; without it, search cannot distinguish between prefixes and full words.
BIt stores the frequency of words; without it, search would be slower.
CIt points to the parent node; without it, traversal would be impossible.
DIt stores the character value; without it, nodes would be empty.
Attempts:
2 left
💡 Hint
Think about how the Trie differentiates between a word and its prefix.