Bird
Raised Fist0

Identify the bug in the following trie-based maximum XOR code snippet:

medium🐞 Bug Identification Q14 of Q15
Bit Manipulation - Maximum XOR of Two Numbers
Identify the bug in the following trie-based maximum XOR code snippet:
ALine with 'node = node.children[bit]' does not check if 'bit' exists in children.
BLine with 'if bit not in node.children:' should check for toggled_bit instead.
CLine with 'max_xor |= (1 << i)' should be outside the if condition.
DLine with 'node = self.root' should be inside the for loop.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze traversal in findMaxXor

    When toggled_bit is not found, code moves to node.children[bit] without checking if bit exists, risking KeyError.
  2. Step 2: Identify missing null check

    Should use node.children.get(bit) or check existence before accessing to avoid runtime error.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Missing check causes crash on inputs missing bit child [OK]
Quick Trick: Always check child existence before traversal [OK]
Common Mistakes:
MISTAKES
  • Assuming child always exists
  • Confusing toggled_bit check
  • Misplacing max_xor update
Trap Explanation:
PITFALL
  • Code looks correct but misses null check causing runtime errors on some inputs.
Interviewer Note:
CONTEXT
  • Tests attention to detail and understanding of trie traversal edge cases.
Master "Maximum XOR of Two Numbers" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes