XOR Trick – Common Interview Follow-up Questions

1. Why can’t we extend the XOR trick to three or more odd-occurring numbers?

Short Answer

The XOR trick works because XOR only gives us one combined piece of information.

  • With one odd-occurring number:

    • xor_all = a

    • We directly get the answer.

  • With two odd-occurring numbers:

    • xor_all = a ^ b

    • Since a ≠ b, there is at least one bit where they differ.

    • That differing bit guarantees that a and b will fall into different groups.

    • XOR inside each group isolates each number.

  • With three or more odd-occurring numbers:

    • xor_all = a ^ b ^ c ...

    • A differing bit no longer guarantees that every odd-occurring number gets separated.

    • One partition may still contain multiple odd-occurring numbers, leaving us with another XOR expression instead of an individual answer.


Simple Interview Example

Suppose the odd-occurring numbers are:

5 = 0101
7 = 0111
9 = 1001

Overall XOR:

0101
0111
1001
----
1011

Choose the rightmost set bit (0001) to partition.

Groups become:

Group 1:
5
7
9

Group 2:
(empty)

Now XOR Group 1:

5 ^ 7 ^ 9

We still have three unknowns together.

Nothing gets isolated.

The trick fails because the partition is no longer guaranteed to separate all unknowns.


Interview One-Liner

The XOR trick works for exactly two odd-occurring numbers because any set bit in a ^ b guarantees that a and b fall into different groups. With three or more odd-occurring numbers, this guarantee disappears—multiple odd-occurring numbers can remain in the same partition, so XOR cannot isolate them.


Easy Analogy (Great for Interviews)

Imagine there are 2 criminals.

A clue says:

One is north of the river and one is south.

You immediately know they are separated.

Now imagine there are 3 criminals.

The same clue only tells you:

At least one is north and at least one is south.

Possible distributions:

North : 1
South : 2

or

North : 2
South : 1

You still don’t know who is where.

Exactly the same limitation exists with XOR.


2. Can the XOR of one or two odd-occurring numbers become zero? If it becomes then algorithm fails , right?

Case 1: One odd-occurring number

After all even-occurring numbers cancel,

xor_all = odd_element

So XOR becomes zero only if the odd-occurring element itself is 0.

Example

Array:
2 2 5 5 0

XOR:

2 ^ 2 ^ 5 ^ 5 ^ 0

= 0

Here, the odd-occurring element is actually 0.


Case 2: Two odd-occurring numbers

Suppose the answers are:

a
b

After cancellation,

xor_all = a ^ b

Can this be zero?

No.

Because

x ^ y = 0

if and only if

x == y

But the problem states that the two odd-occurring numbers are distinct.

Therefore,

a ^ b ≠ 0

There will always be at least one set bit available for partitioning.


Simple Interview Example

Suppose

5 = 0101
7 = 0111
5 ^ 7

0101
0111
----
0010

Result is not zero.

If someone says

5 ^ 5 = 0

then those are not two different odd-occurring numbers—they’re the same number, which violates the problem statement.


Case 3: Three or More Odd-Occurring Numbers

Here, XOR can become zero.

Example:

1 = 001
2 = 010
3 = 011
001
010
011
---
000

So,

1 ^ 2 ^ 3 = 0

This is another reason why the XOR trick cannot be extended to the general case.


Interview Takeaways

  • x ^ y = 0 iff x == y.

  • For one odd-occurring number:

    • xor_all equals that number.

    • It is zero only if the answer itself is 0.

  • For two distinct odd-occurring numbers:

    • xor_all can never be zero.

    • Therefore, there is always at least one set bit to partition the array.

  • For three or more odd-occurring numbers:

    • XOR may become zero or otherwise lose too much information.

    • A partition is no longer guaranteed to isolate the unknown numbers.

Local Graph View

Start typing to search
Try: two sum or #Arrays or #Amazon