0
0
DSA Typescriptprogramming~20 mins

Longest Word in Dictionary Using Trie in DSA Typescript - 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 Longest Word Found Using Trie
What is the output of the following TypeScript 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 Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEnd: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

  insert(word: string): void {
    let node = this.root;
    for (const char of word) {
      if (!node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char)!;
    }
    node.isEnd = true;
  }

  longestWord(): string {
    let result = "";
    const dfs = (node: TrieNode, path: string): void => {
      if (path.length > result.length || (path.length === result.length && path < result)) {
        result = path;
      }
      for (const [char, child] of [...node.children.entries()].sort((a, b) => a[0].localeCompare(b[0]))) {
        if (child.isEnd) {
          dfs(child, path + char);
        }
      }
    };
    dfs(this.root, "");
    return result;
  }
}

const words = ["a", "app", "ap", "apply", "apple", "appl", "b", "ban", "banana"];
const trie = new Trie();
for (const w of words) {
  trie.insert(w);
}
console.log(trie.longestWord());
A"apple"
B"apply"
C"appl"
D"banana"
Attempts:
2 left
💡 Hint
Think about which word has all its prefixes present in the list and is the longest.
🧠 Conceptual
intermediate
1:00remaining
Understanding Trie Node Structure
Which of the following best describes the purpose of the 'isEnd' property in a Trie node when finding the longest word in a dictionary?
AIt points to the parent node in the Trie.
BIt counts how many words pass through this node.
CIt stores the character value of the node.
DIt marks the node as the end of a valid word in the dictionary.
Attempts:
2 left
💡 Hint
Think about how the Trie knows a word ends at a node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Trie Longest Word Search
The following code attempts to find the longest word in a dictionary using a Trie, but it returns an empty string. What is the bug?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEnd: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

  insert(word: string): void {
    let node = this.root;
    for (const char of word) {
      if (!node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char)!;
    }
    node.isEnd = true;
  }

  longestWord(): string {
    let result = "";
    const dfs = (node: TrieNode, path: string): void => {
      if (path.length > result.length || (path.length === result.length && path < result)) {
        result = path;
      }
      for (const [char, child] of node.children.entries()) {
        if (child.isEnd) {
          dfs(child, path + char);
        }
      }
    };
    dfs(this.root, "");
    return result;
  }
}

const words = ["a", "app", "ap", "apply", "apple", "appl"];
const trie = new Trie();
for (const w of words) {
  trie.insert(w);
}
console.log(trie.longestWord());
AThe root node's isEnd is false, so DFS starts from a node that is not a valid prefix, causing no updates to result.
BThe DFS does not sort children, so traversal order is random and may miss the lexicographically smallest word.
CThe insert method does not mark the last node as isEnd = true, so no words are recognized as complete.
DThe DFS does not check if the current node is the root before updating the result, causing empty string to be returned.
Attempts:
2 left
💡 Hint
Consider what happens at the root node and how the DFS updates the result.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in Trie Implementation
Which option contains a syntax error that will prevent the TypeScript Trie code from compiling?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isEnd: boolean = false;
}

class Trie {
  root: TrieNode = new TrieNode();

  insert(word: string): void {
    let node = this.root;
    for (const char of word) {
      if (!node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char)!;
    }
    node.isEnd = true;
  }

  longestWord(): string {
    let result = "";
    const dfs = (node: TrieNode, path: string): void => {
      if (path.length > result.length || (path.length === result.length && path < result)) {
        result = path;
      }
      for (const [char, child] of [...node.children.entries()].sort()) {
        if (child.isEnd) {
          dfs(child, path + char);
        }
      }
    };
    dfs(this.root, "");
    return result;
  }
}
AMissing semicolon after 'node.isEnd = true' in insert method.
BUsing 'sort()' directly on Map entries without a compare function causes a syntax error.
CThe arrow function in dfs is missing a return type annotation.
DThe exclamation mark '!' after get(char) is invalid syntax in TypeScript.
Attempts:
2 left
💡 Hint
Check how sorting is applied to Map entries in TypeScript.
🚀 Application
expert
2:30remaining
Longest Word with All Prefixes Present - Result Count
Given the following list of words inserted into a Trie, how many words in the list qualify as the longest word where all prefixes are also present in the dictionary?
DSA Typescript
const words = ["a", "ap", "app", "appl", "apple", "apply", "b", "ban", "bana", "banan", "banana"];
// Assume Trie insertion and longest word logic as usual.
A1
B3
C2
D4
Attempts:
2 left
💡 Hint
Check which words have all prefixes present and are the longest among them.