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 the following Go code that uses XOR to find the only non-repeating element in the array?
DSA Go
package main import "fmt" func main() { arr := []int{2, 3, 5, 4, 5, 3, 4} result := 0 for _, v := range arr { result ^= v } fmt.Println(result) }
Attempts:
2 left
💡 Hint
XOR of a number with itself is 0, and XOR with 0 is the number itself.
✗ Incorrect
All repeating numbers cancel out because XOR of a number with itself is zero. Only the non-repeating number remains.
❓ Predict Output
intermediateResult of XOR with multiple pairs and one unique
What will be printed by this Go program that finds the unique element using XOR?
DSA Go
package main import "fmt" func main() { nums := []int{10, 14, 10, 14, 20} unique := 0 for _, num := range nums { unique ^= num } fmt.Println(unique) }
Attempts:
2 left
💡 Hint
Pairs cancel out, leaving the unique number.
✗ Incorrect
10 XOR 10 = 0, 14 XOR 14 = 0, so only 20 remains after XOR.
🧠 Conceptual
advancedWhy XOR helps find the unique element?
Why does XOR operation help in finding the only non-repeating element in an array where every other element repeats twice?
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number twice.
✗ Incorrect
XOR cancels out pairs because n ^ n = 0, and 0 ^ n = n, so only the unique number remains.
❓ Predict Output
advancedOutput when array has multiple unique elements
What will be the output of this Go code that uses XOR on an array with two unique elements?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 2, 1, 4} res := 0 for _, val := range arr { res ^= val } fmt.Println(res) }
Attempts:
2 left
💡 Hint
XOR of two unique numbers is their combined XOR value.
✗ Incorrect
1 and 2 appear twice and cancel out. XOR of 3 and 4 is 7, so output is 7.
🔧 Debug
expertIdentify the error in XOR unique element code
What error will this Go code produce when trying to find the unique element using XOR?
DSA Go
package main import "fmt" func main() { arr := []int{5, 7, 5, 9, 7} var result int for i := 0; i < len(arr); i++ { result ^= arr[i] } fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop uses i <= len(arr), which causes index out of range error because valid indices are 0 to len(arr)-1.
