Challenge - 5 Problems
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of XOR-based two unique elements finder
What is the output of the following Python code that finds two non-repeating elements in an array using XOR?
DSA Python
def find_two_unique(arr): xor_all = 0 for num in arr: xor_all ^= num set_bit = xor_all & (-xor_all) num1 = 0 num2 = 0 for num in arr: if num & set_bit: num1 ^= num else: num2 ^= num return num1, num2 result = find_two_unique([2, 3, 7, 9, 2, 3]) print(result)
Attempts:
2 left
💡 Hint
Remember XOR of two same numbers is zero and XOR of all elements gives XOR of unique elements.
✗ Incorrect
The code uses XOR to find the XOR of the two unique numbers (7 and 9). Then it finds a set bit to separate numbers into two groups and XORs them separately to get the unique numbers. The order returned is (7, 9).
❓ Predict Output
intermediate1:30remaining
Output of XOR partitioning step
What is the value of 'set_bit' after executing this code snippet?
DSA Python
arr = [4, 1, 2, 1, 2, 5] xor_all = 0 for num in arr: xor_all ^= num set_bit = xor_all & (-xor_all) print(set_bit)
Attempts:
2 left
💡 Hint
The set_bit isolates the rightmost set bit of xor_all.
✗ Incorrect
xor_all is XOR of all elements: 4^1^2^1^2^5 = 4^0^0^5 = 1 (since pairs cancel out). The rightmost set bit of 1 is 1 itself.
🔧 Debug
advanced2:00remaining
Identify the error in XOR two unique elements code
What error will this code produce when run?
DSA Python
def find_two_unique(arr): xor_all = 0 for num in arr: xor_all ^= num set_bit = xor_all & (-xor_all) num1 = 0 num2 = 0 for num in arr: if num & set_bit: num1 ^= num else: num2 ^= num return num1, num2 print(find_two_unique([1, 2, 1, 3]))
Attempts:
2 left
💡 Hint
Check the for loop syntax carefully.
✗ Incorrect
The for loop is missing a colon after 'for num in arr', causing a SyntaxError.
❓ Predict Output
advanced2:00remaining
Output of XOR unique elements with negative numbers
What is the output of this code that finds two unique elements in an array containing negative numbers?
DSA Python
def find_two_unique(arr): xor_all = 0 for num in arr: xor_all ^= num set_bit = xor_all & (-xor_all) num1 = 0 num2 = 0 for num in arr: if num & set_bit: num1 ^= num else: num2 ^= num return num1, num2 result = find_two_unique([-1, -2, -1, -3, -2, -4]) print(result)
Attempts:
2 left
💡 Hint
XOR works with negative numbers as well.
✗ Incorrect
The unique elements are -3 and -4. The function returns them in the order (-3, -4).
🚀 Application
expert1:30remaining
Number of items in dictionary after XOR grouping
After running the following code, how many keys will the dictionary 'groups' contain?
DSA Python
arr = [10, 12, 10, 15, 12, 20] xor_all = 0 for num in arr: xor_all ^= num set_bit = xor_all & (-xor_all) groups = {} for num in arr: key = 'group1' if num & set_bit else 'group2' groups[key] = groups.get(key, 0) + 1 print(len(groups))
Attempts:
2 left
💡 Hint
There are two groups based on the set bit.
✗ Incorrect
The dictionary 'groups' will have two keys: 'group1' and 'group2', so length is 2.