0
0
DSA Goprogramming~20 mins

Longest Word in Dictionary Using Trie in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery: Longest Word Finder
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Longest Word Search in Trie
What is the output of the following Go code that builds a Trie from a list of words and finds the longest word where all prefixes are also words in the list?
DSA Go
package main

import "fmt"

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

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

// Insert inserts a word into the Trie
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.isWord = true
}

// LongestWord finds the longest word with all prefixes present
func (t *Trie) LongestWord() string {
    var result string
    var dfs func(node *TrieNode, path []rune)
    dfs = func(node *TrieNode, path []rune) {
        if len(path) > len(result) || (len(path) == len(result) && string(path) < result) {
            result = string(path)
        }
        for ch := range node.children {
            child := node.children[ch]
            if child.isWord {
                dfs(child, append(path, ch))
            }
        }
    }
    dfs(t.root, []rune{})
    return result
}

func main() {
    words := []string{"a", "app", "ap", "apply", "apple", "appl"}
    trie := &Trie{root: &TrieNode{children: make(map[rune]*TrieNode)}}
    for _, w := range words {
        trie.Insert(w)
    }
    fmt.Println(trie.LongestWord())
}
A"apply"
B"app"
C"apple"
D"appl"
Attempts:
2 left
💡 Hint
Check which words have all their prefixes also present in the list.
🧠 Conceptual
intermediate
1:00remaining
Understanding Trie Node Structure
Which of the following best describes the purpose of the 'isWord' boolean in a Trie node when finding the longest word in a dictionary?
AIt indicates if the node has any children nodes.
BIt counts how many words pass through this node.
CIt stores the character value of the node.
DIt marks whether the node represents the end of a valid word in the dictionary.
Attempts:
2 left
💡 Hint
Think about how to know if a prefix is a complete word.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Trie Longest Word Search
What error will occur if the DFS function in the Trie LongestWord method does not check if child.isWord before recursing?
DSA Go
func (t *Trie) LongestWord() string {
    var result string
    var dfs func(node *TrieNode, path []rune)
    dfs = func(node *TrieNode, path []rune) {
        if len(path) > len(result) || (len(path) == len(result) && string(path) < result) {
            result = string(path)
        }
        for ch := range node.children {
            child := node.children[ch]
            // Missing check: if child.isWord
            dfs(child, append(path, ch))
        }
    }
    dfs(t.root, []rune{})
    return result
}
AThe DFS will never terminate, causing infinite recursion.
BThe function will include incomplete prefixes, resulting in incorrect longest word output.
CThe code will produce a compilation error due to missing isWord check.
DThe code will cause a runtime panic due to nil pointer dereference.
Attempts:
2 left
💡 Hint
Consider what happens if prefixes are not validated as words.
🚀 Application
advanced
1:30remaining
Longest Word Result After Insertions
Given the words inserted into the Trie: ["w", "wo", "wor", "worl", "world", "word", "war"], what will be the longest word returned by the LongestWord method?
A"world"
B"word"
C"war"
D"worl"
Attempts:
2 left
💡 Hint
Check which words have all prefixes present and are longest.
🧠 Conceptual
expert
1:00remaining
Lexicographical Order in Longest Word Selection
Why does the LongestWord method compare strings lexicographically when lengths are equal during DFS?
ATo ensure the lexicographically smallest word is chosen among words of the same length.
BTo speed up the DFS traversal by pruning branches early.
CTo avoid visiting nodes that do not form valid words.
DTo count the total number of words stored in the Trie.
Attempts:
2 left
💡 Hint
Think about tie-breaking when multiple words have the same length.