0
0
DSA Javascriptprogramming~20 mins

Trie vs Hash Map for Prefix Matching in DSA Javascript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prefix Search Using Trie
What is the output of the following JavaScript code that uses a Trie to find words with a given prefix?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = {};
    this.isWord = 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.isWord = 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.isWord) {
        results.push(path);
      }
      for (const c in currNode.children) {
        dfs(currNode.children[c], path + c);
      }
    };
    dfs(node, prefix);
    return results;
  }
}

const trie = new Trie();
['cat', 'car', 'cart', 'dog', 'dove'].forEach(word => trie.insert(word));
console.log(trie.startsWith('car'));
A["car", "cart"]
B["cat", "car", "cart"]
C[]
D["cart"]
Attempts:
2 left
💡 Hint
Think about which words start exactly with the prefix 'car'.
🧠 Conceptual
intermediate
1:30remaining
Why Use Trie Over Hash Map for Prefix Matching?
Which of the following is the main advantage of using a Trie over a Hash Map for prefix matching?
AHash Map can store duplicate keys which Trie cannot.
BHash Map uses less memory than Trie for storing prefixes.
CTrie allows efficient prefix search without checking every key explicitly.
DTrie requires less code to implement than Hash Map.
Attempts:
2 left
💡 Hint
Consider how prefix search works in both data structures.
Predict Output
advanced
2:00remaining
Output of Prefix Search Using Hash Map
What is the output of the following JavaScript code that uses a Hash Map (object) to find words with a given prefix?
DSA Javascript
const words = ['apple', 'app', 'apricot', 'banana', 'bat'];
const wordMap = {};
words.forEach(word => { wordMap[word] = true; });

function findPrefix(prefix) {
  const results = [];
  for (const word in wordMap) {
    if (word.startsWith(prefix)) {
      results.push(word);
    }
  }
  return results;
}

console.log(findPrefix('ap'));
A["apple", "app", "apricot"]
B["apple", "app"]
C["apricot"]
D[]
Attempts:
2 left
💡 Hint
Check which words start with 'ap'.
🔧 Debug
advanced
2:00remaining
Identify the Error in Trie Insertion
What error will the following code produce when inserting words into a Trie?
DSA Javascript
class TrieNode {
  constructor() {
    this.children = new Map();
    this.isWord = false;
  }
}

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

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

const trie = new Trie();
trie.insert('hello');
ATypeError: node.children[char] is not a function
BSyntaxError: Unexpected token
CNo error, code runs correctly
DTypeError: node.children[char] is undefined
Attempts:
2 left
💡 Hint
Check how children are accessed when children is a Map.
🧠 Conceptual
expert
2:30remaining
Time Complexity Comparison for Prefix Search
Which statement correctly compares the time complexity of prefix search in a Trie versus a Hash Map storing N words each of average length L?
ATrie prefix search is O(1), Hash Map prefix search is O(L)
BTrie prefix search is O(L), Hash Map prefix search is O(N * L)
CTrie prefix search is O(N), Hash Map prefix search is O(L)
DBoth Trie and Hash Map prefix searches are O(N * L)
Attempts:
2 left
💡 Hint
Consider how many characters and words are checked in each data structure.