0
0
DSA Goprogramming~20 mins

Prefix Search Using 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 Prefix Search Result
What is the output of the following Go code that inserts words into a trie and searches for words with prefix "app"?
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) StartsWith(prefix string) []string {
	var results []string
	var dfs func(node *TrieNode, path []rune)

	node := t.root
	for _, ch := range prefix {
		if node.children[ch] == nil {
			return results
		}
		node = node.children[ch]
	}

	dfs = func(node *TrieNode, path []rune) {
		if node.isEnd {
			results = append(results, prefix+string(path))
		}
		for ch, child := range node.children {
			dfs(child, append(path, ch))
		}
	}

	dfs(node, []rune{})
	return results
}

func main() {
	trie := NewTrie()
	words := []string{"apple", "app", "application", "apt", "banana"}
	for _, w := range words {
		trie.Insert(w)
	}
	result := trie.StartsWith("app")
	fmt.Println(result)
}
A["app", "apple", "application"]
B["apple", "application"]
C["app", "apt", "apple", "application"]
D["app", "apple", "application", "apt"]
Attempts:
2 left
💡 Hint
Focus on words that start exactly with the prefix "app".
🧠 Conceptual
intermediate
1:30remaining
Number of Nodes in Trie After Insertions
After inserting the words ["cat", "car", "cart", "dog"] into an empty trie, how many nodes does the trie contain (including the root node)?
A9
B8
C7
D10
Attempts:
2 left
💡 Hint
Count shared prefixes only once.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Search Function
What error does the following Go code produce when searching for prefix "bat" after inserting words ["bat", "bath", "batman"]?
DSA Go
func (t *Trie) StartsWith(prefix string) []string {
	var results []string
	var dfs func(node *TrieNode, path []rune)

	node := t.root
	for _, ch := range prefix {
		if node.children[ch] == nil {
			return results
		}
		node = node.children[ch]
	}

	dfs = func(node *TrieNode, path []rune) {
		if node.isEnd {
			results = append(results, string(path))
		}
		for ch, child := range node.children {
			dfs(child, append(path, ch))
		}
	}

	dfs(node, []rune{})
	return results
}
AThe code returns an empty list even though words exist
BThe code causes a runtime panic due to nil pointer dereference
CThe returned words miss the prefix "bat" in their output
DThe code compiles but returns duplicate words
Attempts:
2 left
💡 Hint
Check how the prefix is combined with the path in the DFS function.
🚀 Application
advanced
1:30remaining
Find All Words With Prefix "pre"
Given a trie with words ["prefix", "preach", "prevent", "press", "present"], what is the output of searching for prefix "pre"?
A["preach", "prevent", "press", "present"]
B["prefix", "preach", "prevent", "press", "present"]
C["prefix", "prevent", "press", "present"]
D["prefix", "preach", "press", "present"]
Attempts:
2 left
💡 Hint
All words start with "pre" so all should be included.
🧠 Conceptual
expert
1:30remaining
Trie Memory Efficiency Compared to Hash Map
Why is a trie often more memory efficient than storing all words in a hash map when many words share prefixes?
ABecause tries use less memory for storing keys as strings compared to hash maps
BBecause tries compress all words into a single string internally
CBecause tries avoid storing any characters and only store word counts
DBecause tries store shared prefixes only once, reducing duplicate storage
Attempts:
2 left
💡 Hint
Think about how common parts of words are stored in tries.