Bird
Raised Fist0

What is the output of the XOR and partition code when the input is [0, 0, -1, -2]?

medium🧾 Code Trace Q4 of Q15
Bit Manipulation - Single Number III (Two Singles)
What is the output of the XOR and partition code when the input is [0, 0, -1, -2]?
A[0, -1]
B[-2, -1]
C[-1, -2]
D[0, 0]
Step-by-Step Solution
Solution:
  1. Step 1: XOR all elements

    0 ^ 0 ^ -1 ^ -2 = (-1) ^ (-2) = 1 (binary 1)
  2. Step 2: Find rightmost set bit

    1 & (-1) = 1 (binary 1)
  3. Step 3: Partition and XOR groups

    Group with bit set: -1; Group without bit set: 0, 0, -2 -> XOR = -2
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Output correctly identifies unique negatives [OK]
Quick Trick: Handle negative numbers carefully with two's complement [OK]
Common Mistakes:
MISTAKES
  • Misinterpreting negative bit operations
  • Assuming set bit always positive
Trap Explanation:
PITFALL
  • Candidates often mishandle negative numbers in bitwise operations causing wrong partitioning.
Interviewer Note:
CONTEXT
  • Tests edge case handling of negative numbers in XOR partition.
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