Bird
Raised Fist0

What is the value of xor_all just before the return statement when the input is [3, 0, 1]?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Find the Missing Number
Consider the following code to find the missing number in an array containing numbers from 0 to n with one missing. What is the value of xor_all just before the return statement when the input is [3, 0, 1]?
A2
B3
C0
D1
Step-by-Step Solution
  1. Step 1: Trace xor_all after first loop (XOR 0 to 3)

    0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 2 = 3, 3 ^ 3 = 0
  2. Step 2: XOR xor_all with each array element

    0 ^ 3 = 3, 3 ^ 0 = 3, 3 ^ 1 = 2
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Final xor_all is 2, the missing number [OK]
Quick Trick: XOR all indices and elements cancels pairs leaving missing number [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in range causing wrong xor_all
  • XORing only array or only indices
  • Misreading xor_all updates
Trap Explanation:
PITFALL
  • Candidates often forget that XORing 0 to n includes n, or confuse order of XOR operations.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to mentally execute XOR logic and boundary conditions
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