Bird
Raised Fist0

What is the output of the following code snippet when called with input [4, 1, 2, 1, 2]?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - Single Number (XOR Self-Cancellation)
What is the output of the following code snippet when called with input [4, 1, 2, 1, 2]?
A4
B1
C2
D0
Step-by-Step Solution
Solution:
  1. Step 1: Trace XOR operations

    0 · 4 = 4, 4 · 1 = 5, 5 · 2 = 7, 7 · 1 = 6, 6 · 2 = 4
  2. Step 2: Final result

    The XOR of all elements is 4, the unique element.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Result matches unique element 4 [OK]
Quick Trick: XOR all elements to isolate unique number [OK]
Common Mistakes:
MISTAKES
  • Miscounting XOR steps
  • Off-by-one in loop
Trap Explanation:
PITFALL
  • Candidates often miscalculate XOR sequence or forget initial zero.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute XOR-based code.
Master "Single Number (XOR Self-Cancellation)" 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