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 Finder
What is the output of the following Go code that finds two non-repeating elements in an array using XOR?
DSA Go
package main import "fmt" func findTwoNonRepeating(arr []int) (int, int) { xor := 0 for _, v := range arr { xor ^= v } setBit := xor & -xor x, y := 0, 0 for _, v := range arr { if v&setBit != 0 { x ^= v } else { y ^= v } } return x, y } func main() { arr := []int{2, 3, 7, 9, 2, 3} a, b := findTwoNonRepeating(arr) fmt.Println(a, b) }
Attempts:
2 left
💡 Hint
Focus on how XOR helps isolate unique elements and how the set bit divides the array.
✗ Incorrect
The XOR of all elements gives xor = 7 ^ 9. The set bit isolates one differing bit between 7 and 9. Partitioning the array by this bit and XORing each group gives the two unique numbers 7 and 9. The order is consistent with the first unique number found in the partition with the set bit.
🧠 Conceptual
intermediateWhy 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 all repeating elements cancel out. The XOR of the entire array then equals the XOR of the two unique elements, which helps in isolating them.
🔧 Debug
advancedIdentify the Bug in XOR Partitioning Step
What is the bug in this partitioning step of the code to find two unique elements?
for _, v := range arr {
if v & xor != 0 {
x ^= v
} else {
y ^= v
}
}
DSA Go
xor := 7 ^ 9 x, y := 0, 0 for _, v := range arr { if v & xor != 0 { x ^= v } else { y ^= v } }
Attempts:
2 left
💡 Hint
Check how the partition bit is chosen from xor.
✗ Incorrect
The partitioning must use the rightmost set bit of xor (setBit = xor & -xor) to separate elements. Using xor directly checks multiple bits and partitions incorrectly.
❓ Predict Output
advancedOutput of XOR Two Unique Elements with Negative Numbers
What is the output of this Go code that finds two unique elements in an array containing negative numbers?
DSA Go
package main import "fmt" func findTwoNonRepeating(arr []int) (int, int) { xor := 0 for _, v := range arr { xor ^= v } setBit := xor & -xor x, y := 0, 0 for _, v := range arr { if v&setBit != 0 { x ^= v } else { y ^= v } } return x, y } func main() { arr := []int{-1, -2, -1, -3, -2, -4} a, b := findTwoNonRepeating(arr) fmt.Println(a, b) }
Attempts:
2 left
💡 Hint
XOR works the same with negative numbers; focus on partitioning by set bit.
✗ Incorrect
The code correctly finds the two unique negative numbers -3 and -4 by XOR and partitioning using the rightmost set bit.
🚀 Application
expertNumber of Items in Result After XOR Partitioning
Given an array where all elements except two appear twice, after XORing all elements and partitioning by the rightmost set bit, how many elements will be in each partition?
Attempts:
2 left
💡 Hint
Consider how repeated elements are distributed based on the set bit.
✗ Incorrect
Repeated elements appear twice and share the same bit pattern at the set bit, so they go to the same partition twice and cancel out. Each partition ends up with one unique element plus repeated elements that cancel out.
