Challenge - 5 Problems
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code using XOR to find the unique element?
Consider the Rust code below that finds the only non-repeating element in an array using XOR. What will it print?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
let mut result = 0;
for &num in arr {
result ^= num;
}
result
}
fn main() {
let nums = [2, 3, 5, 4, 5, 3, 4];
println!("{}", find_unique(&nums));
}Attempts:
2 left
💡 Hint
XOR of a number with itself is zero, and XOR with zero is the number itself.
✗ Incorrect
The XOR operation cancels out all numbers appearing twice, leaving only the unique number. Here, 2 appears once, so the output is 2.
❓ Predict Output
intermediateWhat is the output of this Rust code with negative numbers?
What will this Rust program print when finding the unique element using XOR with negative numbers?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
let mut result = 0;
for &num in arr {
result ^= num;
}
result
}
fn main() {
let nums = [-1, -2, -2, -3, -1];
println!("{}", find_unique(&nums));
}Attempts:
2 left
💡 Hint
XOR works the same with negative numbers as with positive numbers.
✗ Incorrect
Pairs cancel out, leaving -3 as the unique element.
🔧 Debug
advancedWhat error does this Rust code produce?
This Rust code tries to find the unique element using XOR but has a mistake. What error will it cause?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
let mut result = 0;
for num in arr {
result ^= num;
}
result
}
fn main() {
let nums = [1, 2, 1];
println!("{}", find_unique(&nums));
}Attempts:
2 left
💡 Hint
Check the type of 'num' in the for loop and how XOR is applied.
✗ Incorrect
The for loop iterates over references (&i32), but XOR expects i32 values. The code must dereference num.
🧠 Conceptual
advancedWhy does XOR help find the unique element in an array where others repeat twice?
Which statement best explains why XOR can find the only non-repeating element in an array where every other element appears exactly 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. XOR with zero returns the number itself, leaving the unique element.
❓ Predict Output
expertWhat is the output of this Rust code with multiple unique elements?
What will this Rust program print when the array has two unique elements and others repeated twice?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
let mut result = 0;
for &num in arr {
result ^= num;
}
result
}
fn main() {
let nums = [1, 2, 3, 2, 1, 4];
println!("{}", find_unique(&nums));
}Attempts:
2 left
💡 Hint
XOR of two unique numbers is their XOR combination, not one of them.
✗ Incorrect
Since there are two unique numbers (3 and 4), XOR returns 3 ^ 4 = 7.
