0
0
DSA Typescriptprogramming~20 mins

Why Trie Exists and What Hash Map Cannot Do for Strings in DSA Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trie Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is a Trie preferred over a Hash Map for prefix searches?
Imagine you want to find all words starting with 'app' in a large dictionary. Why is a Trie better than a Hash Map for this task?
ATrie stores characters in a tree structure allowing efficient prefix search, while Hash Map cannot efficiently find keys by prefix.
BHash Map uses less memory than Trie, so it is better for prefix searches.
CTrie is slower than Hash Map for prefix searches because it stores entire words.
DHash Map can find prefixes faster because it hashes the prefix directly.
Attempts:
2 left
💡 Hint

Think about how each data structure organizes keys and how that affects searching by starting letters.

Predict Output
intermediate
2:00remaining
Output of Trie prefix search vs Hash Map lookup
Given the following TypeScript code, what is the output?
DSA Typescript
class TrieNode {
  children: Map<string, TrieNode> = new Map();
  isWord = false;
}

class Trie {
  root = new TrieNode();

  insert(word: string) {
    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.isWord = true;
  }

  startsWith(prefix: string): boolean {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children.has(char)) return false;
      node = node.children.get(char)!;
    }
    return true;
  }
}

const trie = new Trie();
trie.insert('apple');
trie.insert('app');

const hashMap = new Map<string, boolean>();
hashMap.set('apple', true);
hashMap.set('app', true);

console.log(trie.startsWith('app'));
console.log(hashMap.has('app'));
console.log(hashMap.has('ap'));
console.log(trie.startsWith('ap'));
A
true
true
false
true
B
true
true
true
true
C
false
true
false
false
D
true
false
false
true
Attempts:
2 left
💡 Hint

Check how Trie and Hash Map handle prefixes and exact keys differently.

🔧 Debug
advanced
2:00remaining
Why does this Trie prefix search fail for some prefixes?
Find the bug in this TypeScript Trie startsWith method that causes it to return false for some prefixes that exist.
DSA Typescript
class TrieNode {
  children: {[key: string]: TrieNode} = {};
  isWord = false;
}

class Trie {
  root = new TrieNode();

  insert(word: string) {
    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: string): boolean {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children[char]) return false;
      node = node.children[char];
    }
    return true;
  }
}

const trie = new Trie();
trie.insert('hello');
console.log(trie.startsWith('hel'));
AThe startsWith method should check for isWord at each character.
BThe startsWith method incorrectly returns node.isWord instead of true after prefix traversal.
CThe children property should be a Map instead of an object.
DThe insert method does not add nodes for each character properly.
Attempts:
2 left
💡 Hint

Think about what startsWith should return after successfully finding the prefix path.

Predict Output
advanced
2:00remaining
Memory usage difference between Trie and Hash Map for many strings
Consider storing 1000 English words in a Trie and a Hash Map. Which statement about memory usage is true?
AHash Map uses less memory because it stores only keys without extra nodes.
BBoth use the same memory because they store all words fully.
CTrie uses less memory because it shares common prefixes among words.
DTrie uses more memory because it stores each character as a separate node.
Attempts:
2 left
💡 Hint

Think about how words with common beginnings are stored in each structure.

🧠 Conceptual
expert
3:00remaining
Why can't Hash Maps efficiently support autocomplete features like Tries?
Autocomplete requires finding all words starting with a prefix quickly. Why is a Trie better suited than a Hash Map for this feature?
AHash Map automatically sorts keys, making prefix search faster than Trie.
BHash Map can store prefixes as keys, so it is equally efficient as Trie for autocomplete.
CTrie is slower because it needs to check each character, while Hash Map hashes the prefix directly.
DTrie organizes words by characters allowing traversal to prefix node and collecting descendants; Hash Map lacks this structure and requires scanning all keys.
Attempts:
2 left
💡 Hint

Consider how each data structure organizes and accesses keys related by prefixes.