Challenge - 5 Problems
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of XOR-based Two Non Repeating Elements
What is the output of the following C# code that finds two non-repeating elements in an array using XOR?
DSA C#
using System; class Program { static void Main() { int[] arr = {2, 3, 7, 9, 2, 3}; int xor = 0; foreach (int num in arr) { xor ^= num; } int setBit = xor & ~(xor - 1); int x = 0, y = 0; foreach (int num in arr) { if ((num & setBit) != 0) x ^= num; else y ^= num; } Console.WriteLine(x + " -> " + y + " -> null"); } }
Attempts:
2 left
💡 Hint
Focus on how XOR separates the two unique numbers by a set bit.
✗ Incorrect
The XOR of all elements gives xor = 7 ^ 9. The setBit isolates a differing bit between 7 and 9. Partitioning the array by this bit separates 7 and 9, so x and y become 7 and 9 respectively. The output prints them in order found: 7 -> 9 -> null.
❓ Predict Output
intermediateResult of XOR Partitioning Step
After computing xor and setBit in the XOR method, what is the value of setBit for the array {4, 1, 2, 1, 2, 5}?
DSA C#
int[] arr = {4, 1, 2, 1, 2, 5}; int xor = 0; foreach (int num in arr) { xor ^= num; } int setBit = xor & ~(xor - 1); Console.WriteLine(setBit);
Attempts:
2 left
💡 Hint
Look for the rightmost set bit in xor.
✗ Incorrect
XOR of all elements is 4 ^ 1 ^ 2 ^ 1 ^ 2 ^ 5 = 4 ^ 5 = 1 (since 1 ^ 1 and 2 ^ 2 cancel out). The rightmost set bit of 1 is 1, so setBit = 1.
🔧 Debug
advancedIdentify the Bug in XOR Two Non Repeating Elements Code
What error will the following code produce when run, and why?
DSA C#
int[] arr = {1, 2, 1, 3, 2, 5}; int xor = 0; for (int i = 0; i <= arr.Length; i++) { xor ^= arr[i]; } Console.WriteLine(xor);
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop uses i <= arr.Length which tries to access arr[arr.Length], an invalid index causing IndexOutOfRangeException.
🧠 Conceptual
advancedWhy XOR Helps Find Two Unique Elements
Why does XOR operation help in finding two non-repeating elements in an array where all other elements repeat twice?
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number with itself.
✗ Incorrect
XOR of a number with itself is zero, so pairs cancel out. The result is XOR of the two unique numbers, which helps separate them.
🚀 Application
expertNumber of Items in Resulting List After XOR Partition
Given the array {10, 20, 10, 30, 40, 20}, after applying the XOR method to find two unique elements, how many elements are in the list of numbers that have the set bit in the XOR result?
Attempts:
2 left
💡 Hint
Count how many numbers have the set bit in the XOR of unique elements.
✗ Incorrect
XOR of all elements is 30 ^ 40 = 54. The rightmost set bit in 54 is 2 (binary 110110 & ~(110101) = 10). Numbers with this bit set are 10 (1010), 10 (1010), and 30 (11110). So 3 numbers have this bit set.
