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 to find non-repeating element
What is the output of this C++ code that uses XOR to find the only non-repeating element in the array?
DSA C++
int arr[] = {2, 3, 5, 4, 5, 3, 4}; int n = sizeof(arr)/sizeof(arr[0]); int result = 0; for(int i = 0; i < n; i++) { result ^= arr[i]; } std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
XOR of a number with itself is 0, and XOR with 0 is the number itself.
✗ Incorrect
All repeating elements cancel out because XOR of a number with itself is zero. Only the non-repeating element remains.
❓ Predict Output
intermediateResult of XOR with multiple repeating pairs
What will be printed by this code snippet that finds the only non-repeating element using XOR?
DSA C++
int arr[] = {10, 14, 10, 14, 20}; int n = sizeof(arr)/sizeof(arr[0]); int res = 0; for(int i = 0; i < n; i++) { res ^= arr[i]; } std::cout << res << std::endl;
Attempts:
2 left
💡 Hint
Pairs cancel out, leaving the unique number.
✗ Incorrect
10 and 14 appear twice and cancel out, leaving 20 as the unique element.
🔧 Debug
advancedIdentify the error in XOR implementation
What error does this code produce when trying to find the only non-repeating element using XOR?
DSA C++
int arr[] = {1, 2, 1, 3, 2}; int n = sizeof(arr)/sizeof(arr[0]); int result = 0; for(int i = 0; i < n; i++) { result ^= arr[i]; } std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
Check variable initialization before using XOR.
✗ Incorrect
Variable 'result' is used without initialization, causing undefined behavior.
❓ Predict Output
advancedOutput of XOR with negative numbers
What is the output of this code that finds the only non-repeating element using XOR with negative numbers?
DSA C++
int arr[] = {-1, -2, -1, -3, -2}; int n = sizeof(arr)/sizeof(arr[0]); int result = 0; for(int i = 0; i < n; i++) { result ^= arr[i]; } std::cout << result << std::endl;
Attempts:
2 left
💡 Hint
XOR works the same with negative numbers as with positive.
✗ Incorrect
All repeating negative numbers cancel out, leaving -3 as the unique element.
🧠 Conceptual
expertWhy XOR finds the only non-repeating element?
Why does XOR operation help find the only non-repeating element in an array where every other element repeats exactly twice?
Attempts:
2 left
💡 Hint
Think about how XOR behaves with pairs of identical numbers.
✗ Incorrect
XOR cancels out pairs because n ^ n = 0, and 0 ^ n = n, so only the unique element remains after XORing all elements.
