Bird
Raised Fist0

Given the following code snippet, what is the output when input is [3, 0, 1]?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - Find the Missing Number
Given the following code snippet, what is the output when input is [3, 0, 1]?
A2
B3
C1
D0
Step-by-Step Solution
Solution:
  1. Step 1: Trace xor_all after first loop

    n=3, so i in [0,1,2,3]; xor_all = 0^1^2^3 = 0
  2. Step 2: XOR with array elements

    xor_all ^= 3 -> 0^3=3; xor_all ^= 0 -> 3^0=3; xor_all ^= 1 -> 3^1=2
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Output matches missing number 2 [OK]
Quick Trick: XOR cancels pairs, leaving missing number [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in loop range
  • XOR only array or only indices
Trap Explanation:
PITFALL
  • Candidates often forget to XOR both indices and array elements, leading to wrong output.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to mentally execute XOR logic on sample input.
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