Bird
Raised Fist0

The following code attempts to find the missing number using XOR. Which line contains the subtle bug that causes incorrect results when the missing number is n?

medium🐞 Bug Identification Q14 of Q15
Bit Manipulation - Find the Missing Number
The following code attempts to find the missing number using XOR. Which line contains the subtle bug that causes incorrect results when the missing number is n?
ALine 3: initializing xor_all to 0
BLine 7: returning xor_all without modification
CLine 4: iterating only to n-1 instead of n
DLine 6: XORing array elements
Step-by-Step Solution
  1. Step 1: Analyze the loop range for XORing indices

    The loop runs from 0 to n-1 (range(n)), missing the number n itself.
  2. Step 2: Understand impact on missing number detection

    Since n is not XORed, if n is the missing number, it won't be accounted for, causing wrong output.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Changing loop to range(n+1) fixes the bug [OK]
Quick Trick: Loop must include n to XOR all indices correctly [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in loop range
  • Forgetting to XOR n
  • Assuming initialization causes bug
Trap Explanation:
PITFALL
  • Range(n) looks correct but excludes n, a subtle off-by-one error common in indexing problems.
Interviewer Note:
CONTEXT
  • Tests candidate's attention to boundary conditions and off-by-one errors in bit manipulation
Master "Find the Missing Number" 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