Practice
Solution
Step 1: Trace insertions and queries for nums=[1,4,2], limit=3
Initially, trie is empty. For num=1, query returns 0 (no previous numbers), then insert 1. For num=4, query counts pairs with XOR<3 with 1, which is 1 (1^4=5>3, so 0), then insert 4. For num=2, query counts pairs with XOR<3 with {1,4}. 2^1=3 (not less than 3), 2^4=6>3, so 0. Total count is 0+0+0=0, but careful: the code counts pairs less than limit, so XOR<3 means XOR<3, not <=3.Step 2: Adjust for less than limit (3) meaning XOR values 0,1,2
Pairs: (1,4)=5>3 no, (1,2)=3 not less than 3 no, (4,2)=6 no. So count is 0. But code counts pairs as we insert, so final result is 0.Final Answer:
Option C -> Option CQuick Check:
Code counts pairs with XOR < limit, result is 0 for limit=3 [OK]
- Counting pairs with XOR <= limit instead of < limit
- Off-by-one in bit traversal
- Miscounting pairs before insertion
Solution
Step 1: Understand the problem requirements
The problem requires returning up to three lexicographically smallest products for every prefix of the search word efficiently.Step 2: Evaluate algorithmic approaches
Brute force filtering and sorting for each prefix is inefficient. Dynamic programming is not suitable for prefix-based retrieval. A greedy approach without sorting fails lex order requirements. Building a trie with stored top 3 suggestions at each node allows O(m) retrieval where m is prefix length.Final Answer:
Option A -> Option AQuick Check:
Trie with stored suggestions matches problem constraints and efficiency [OK]
- Assuming brute force is efficient enough
- Confusing DP with prefix search
- Ignoring lexicographic order in greedy approach
class TrieNode:
def __init__(self):
self.children = {}
self.is_word = False
class StreamChecker:
def __init__(self, words):
self.root = TrieNode()
self.max_len = max(len(w) for w in words)
self.stream = []
for word in words:
node = self.root
for ch in reversed(word):
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_word = True
def query(self, letter):
self.stream.append(letter)
if len(self.stream) > self.max_len:
self.stream.pop(0)
node = self.root
for ch in reversed(self.stream):
if ch not in node.children:
return False
node = node.children[ch]
if node.is_word:
return True
return False
sc = StreamChecker(["cd", "f", "kl"])
print(sc.query('a'))
print(sc.query('b'))
print(sc.query('c'))
print(sc.query('d'))
Solution
Step 1: Trace the stream buffer after queries
After queries 'a', 'b', 'c', 'd', the stream buffer contains ['a','b','c','d'], but max_len=2, so oldest letters are popped. After 'c', stream=['b','c'], after 'd', stream=['c','d'].Step 2: Traverse trie with reversed stream
For query('d'), reversed stream is ['d','c']. Trie has reversed words: 'dc' for 'cd'. Traversing 'd' then 'c' hits is_word=True at 'c' node, so returns True. However, the query sequence is query('a'), query('b'), query('c'), query('d'). The query('c') returns False because 'c' alone is not a word. The query('d') returns True because 'cd' is in the dictionary and the stream buffer contains ['c','d'].Step 3: Check query('d') return value
Since the query('d') returns True, the correct answer is True.Final Answer:
Option B -> Option BQuick Check:
Stream buffer limited to max word length matches 'cd' suffix [OK]
- Not limiting stream buffer size
- Not reversing words in trie
- Returning false too early
class StreamChecker:
def __init__(self, words):
self.root = TrieNode()
self.max_len = max(len(w) for w in words)
self.stream = []
for word in words:
node = self.root
for ch in word: # Bug here: words not reversed
if ch not in node.children:
node.children[ch] = TrieNode()
node = node.children[ch]
node.is_word = True
def query(self, letter):
self.stream.append(letter)
if len(self.stream) > self.max_len:
self.stream.pop(0)
node = self.root
for ch in reversed(self.stream):
if ch not in node.children:
return False
node = node.children[ch]
if node.is_word:
return True
return False
Solution
Step 1: Identify how trie is built
The code inserts words in forward order instead of reversed order, which breaks suffix matching logic.Step 2: Understand impact on query
Query traverses reversed stream, expecting reversed trie. Since trie is built forward, traversal fails to find matches.Final Answer:
Option A -> Option AQuick Check:
Reversing words during trie build is essential for suffix queries [OK]
- Not reversing words when building trie
- Not limiting stream buffer
- Returning false too early
Solution
Step 1: Understand reuse means pairs can include the same element twice
Counting pairs with i == j means the current number must be considered in queries.Step 2: Modify insertion and query order
Insert the current number into the trie before querying so it can pair with itself and others, enabling reuse counting.Final Answer:
Option A -> Option AQuick Check:
Inserting before querying allows counting pairs including the current element [OK]
- Assuming no change needed
- Inserting after query (misses self pairs)
- Switching to frequency map unnecessarily
