0
0
DSA Goprogramming~10 mins

Why Trie Exists and What Hash Map Cannot Do for Strings in DSA Go - Test Your Knowledge

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

Complete the code to insert a word into the Trie.

DSA Go
func (t *TrieNode) Insert(word string) {
    node := t
    for _, ch := range word {
        if node.children[ch] == nil {
            node.children[ch] = &TrieNode{children: make(map[rune]*TrieNode)}
        }
        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 a single character as key.
Using the wrong variable like node or t instead of ch.
2fill in blank
medium

Complete the code to check if a word exists in the Trie.

DSA Go
func (t *TrieNode) Search(word string) bool {
    node := t
    for _, ch := range word {
        if node.children[[1]] == nil {
            return false
        }
        node = node.children[ch]
    }
    return node.isEnd
}
Drag options to blanks, or click blank then click option'
Aword
Bnode
Cch
Dt
Attempts:
3 left
💡 Hint
Common Mistakes
Checking with the whole word instead of character.
Using wrong variable as key.
3fill in blank
hard

Fix the error in the code to find if any word starts with the given prefix.

DSA Go
func (t *TrieNode) StartsWith(prefix string) bool {
    node := t
    for _, ch := range prefix {
        if node.children[ch] == nil {
            return false
        }
        node = node.children[[1]]
    }
    return true
}
Drag options to blanks, or click blank then click option'
Ach
Bnode
Cprefix
Dt
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole prefix string instead of character.
Using wrong variable like node or t.
4fill in blank
hard

Fill both blanks to create a map comprehension that counts characters in a string.

DSA Go
counts := map[rune]int{}
for _, ch := range word {
    counts[ch] [1] 1
}
result := map[rune]int{ch: counts[ch] [2] 0 for ch := range counts}
Drag options to blanks, or click blank then click option'
A+=
B-=
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= instead of += for counting.
Using - instead of + when copying counts.
5fill in blank
hard

Fill all three blanks to create a filtered map of characters with counts greater than zero.

DSA Go
filtered := map[rune]int{ch: counts[ch] for ch := range counts if counts[ch] [1] 0 && ch [2] 'a' && ch [3] '{' }
Drag options to blanks, or click blank then click option'
A>
B==
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of > for counts.
Using wrong comparison operators for character range.