Bird
Raised Fist0

Given the following code and input, what is the final returned list?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Single Number III (Two Singles)
Given the following code and input, what is the final returned list?
A[2, 5]
B[5, 4]
C[4, 5]
D[2, 4]
Step-by-Step Solution
  1. Step 1: Compute xor of all elements

    xor = 2 ^ 4 ^ 2 ^ 5 = (2 ^ 2) ^ 4 ^ 5 = 0 ^ 4 ^ 5 = 1 (binary 0001)
  2. Step 2: Find rightmost set bit and partition

    set_bit = xor & (-xor) = 1 & (-1) = 1. Partition nums by bit 1: group1 (bit set): 5, group2 (bit not set): 2,4,2. XOR group1 = 5, XOR group2 = 4.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Output matches unique elements [OK]
Quick Trick: Rightmost set bit partitions unique numbers correctly [OK]
Common Mistakes:
MISTAKES
  • Mixing order of output
  • Incorrect xor calculation
  • Miscomputing set bit
Trap Explanation:
PITFALL
  • Candidates often confuse xor or set bit calculation leading to wrong partitioning.
Interviewer Note:
CONTEXT
  • Tests ability to trace bitwise XOR and partitioning logic precisely.
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