Bird
Raised Fist0

Consider this buggy code snippet for finding two unique numbers. Which line contains the subtle bug?

medium🐞 Bug Identification Q14 of Q15
Bit Manipulation - Single Number III (Two Singles)
Consider this buggy code snippet for finding two unique numbers. Which line contains the subtle bug?
ALine 8: set_bit calculation
BLine 6: xor calculation loop
CLine 3: xor initialization
DLine 12: partitioning condition
Step-by-Step Solution
  1. Step 1: Identify how set_bit is computed

    Correct approach uses set_bit = xor & (-xor) to isolate rightmost set bit. Here, set_bit = xor misses this step.
  2. Step 2: Understand impact of bug

    Without isolating rightmost set bit, partitioning fails, XOR groups won't separate unique numbers correctly.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Bug in set_bit calculation breaks partitioning [OK]
Quick Trick: Isolate rightmost set bit with xor & (-xor) [OK]
Common Mistakes:
MISTAKES
  • Using xor directly as set_bit
  • Breaking early before full XOR
  • Misusing partition condition
Trap Explanation:
PITFALL
  • Using xor directly looks plausible but breaks partitioning logic subtly.
Interviewer Note:
CONTEXT
  • Tests if candidate spots subtle bit manipulation bug that breaks correctness.
Master "Single Number III (Two Singles)" 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