0
0
DSA Javascriptprogramming~20 mins

Prefix Search Using Trie in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Search Master
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 code that inserts words into a Trie and searches for words with prefix 'app'?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEnd = false;
  }
}

class Trie {
  constructor() {
    this.root = new TrieNode();
  }

  insert(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) {
        node.children[char] = new TrieNode();
      }
      node = node.children[char];
    }
    node.isEnd = true;
  }

  startsWith(prefix) {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children[char]) {
        return [];
      }
      node = node.children[char];
    }
    const results = [];
    const dfs = (currNode, path) => {
      if (currNode.isEnd) results.push(path);
      for (const c in currNode.children) {
        dfs(currNode.children[c], path + c);
      }
    };
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
['apple', 'app', 'application', 'bat', 'ball'].forEach(w => trie.insert(w));
console.log(trie.startsWith('app'));
A["app", "apple", "application"]
B["apple", "application"]
C["app", "application"]
D["bat", "ball"]
Attempts:
2 left
💡 Hint
Think about all words starting exactly with 'app' including the prefix itself.
🧠 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?
A8
B10
C7
D9
Attempts:
2 left
💡 Hint
Count shared prefixes only once.
🔧 Debug
advanced
1:30remaining
Identify the Error in Trie Search Method
What error does the following Trie search method produce when searching for a prefix that does not exist?
DSA Javascript
startsWith(prefix) {
  let node = this.root;
  for (const char of prefix) {
    node = node.children[char];
  }
  return node !== undefined;
}
ASyntaxError due to missing return statement
BReturns false correctly
CTypeError: Cannot read property 'children' of undefined
DAlways returns true
Attempts:
2 left
💡 Hint
What happens if node.children[char] is undefined and you try to access its children?
🚀 Application
advanced
1:30remaining
Find All Words with Prefix in Trie
Given a Trie with words inserted, which method correctly returns all words starting with a given prefix?
ATraverse to prefix node, then DFS to collect all words from there
BReturn all words in Trie and filter those starting with prefix
CCheck only if prefix is a word in Trie and return it
DReturn empty list if prefix length is less than 3
Attempts:
2 left
💡 Hint
Think about efficient search using Trie structure.
Predict Output
expert
2:30remaining
Output of Complex Trie Prefix Search
What is the output of the following code snippet?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isEnd = false;
  }
}

class Trie {
  constructor() {
    this.root = new TrieNode();
  }

  insert(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children[char]) {
        node.children[char] = new TrieNode();
      }
      node = node.children[char];
    }
    node.isEnd = true;
  }

  startsWith(prefix) {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children[char]) return [];
      node = node.children[char];
    }
    const results = [];
    const dfs = (currNode, path) => {
      if (currNode.isEnd) results.push(path);
      for (const c in currNode.children) {
        dfs(currNode.children[c], path + c);
      }
    };
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
['team', 'teach', 'test', 'tester', 'testing'].forEach(w => trie.insert(w));
console.log(trie.startsWith('te'));
A["tester", "testing"]
B["team", "teach", "test", "tester", "testing"]
C["team", "teach"]
D["test", "tester", "testing"]
Attempts:
2 left
💡 Hint
All words starting with 'te' should be included.