Think about how each data structure organizes keys and how searching by prefix works.
A Trie stores strings as paths from root to leaves, so all words sharing a prefix share the same path. This makes prefix search fast and direct. A Hash Map stores keys independently, so to find all keys with a prefix, you must check each key, which is slow.
import java.util.*; class TrieNode { Map<Character, TrieNode> children = new HashMap<>(); boolean isWord = false; } class Trie { TrieNode root = new TrieNode(); void insert(String word) { TrieNode node = root; for (char c : word.toCharArray()) { node = node.children.computeIfAbsent(c, k -> new TrieNode()); } node.isWord = true; } List<String> searchPrefix(String prefix) { List<String> results = new ArrayList<>(); TrieNode node = root; for (char c : prefix.toCharArray()) { node = node.children.get(c); if (node == null) return results; } dfs(node, new StringBuilder(prefix), results); return results; } void dfs(TrieNode node, StringBuilder path, List<String> results) { if (node.isWord) results.add(path.toString()); for (char c : node.children.keySet()) { path.append(c); dfs(node.children.get(c), path, results); path.deleteCharAt(path.length() - 1); } } } public class Main { public static void main(String[] args) { Trie trie = new Trie(); trie.insert("cat"); trie.insert("car"); trie.insert("dog"); List<String> prefixResults = trie.searchPrefix("ca"); System.out.println(prefixResults); Map<String, Integer> map = new HashMap<>(); map.put("cat", 1); map.put("car", 2); map.put("dog", 3); List<String> mapResults = new ArrayList<>(); for (String key : map.keySet()) { if (key.startsWith("ca")) { mapResults.add(key); } } System.out.println(mapResults); } }
Check how the Trie collects words starting with 'ca' and how the Hash Map filters keys.
The Trie searchPrefix method returns all words starting with 'ca' by traversing nodes. The Hash Map loop filters keys starting with 'ca'. Both print ["cat", "car"].
Map<String, Integer> map = new HashMap<>(); // map filled with 1 million words List<String> results = new ArrayList<>(); for (String key : map.keySet()) { if (key.startsWith("pre")) { results.add(key); } } System.out.println(results.size());
Think about how Hash Map stores keys and what happens when you want to find keys by prefix.
Hash Map stores keys without order or grouping by prefix. To find keys starting with 'pre', it must check every key, which is slow for large data. Trie organizes keys by prefix, making such searches faster.
Remember how to use Java Map methods to insert if absent and get the value.
Option A uses computeIfAbsent which inserts a new TrieNode if key c is missing and returns the node. Option A works but is two steps. Option A is invalid syntax for Map. Option A returns a new TrieNode but does not insert it into the map.
Consider how autocomplete needs to find all words starting with a typed prefix quickly.
Trie organizes words by shared prefixes, so it can quickly find all words starting with a prefix by traversing the tree. Hash Map stores keys independently, so prefix search requires checking every key, which is slow for autocomplete.
