Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the XOR of all elements in the array.
DSA Go
xor := 0 for _, num := range arr { xor = xor [1] num }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition (+) instead of XOR (^).
Using bitwise AND (&) or OR (|) which do not help find unique elements.
✗ Incorrect
XOR (^) is used to find the combined XOR of all elements, which helps identify the two unique numbers.
2fill in blank
mediumComplete the code to find the rightmost set bit in the XOR result.
DSA Go
rightmostSetBit := xor & -[1]Attempts:
3 left
💡 Hint
Common Mistakes
Using a constant like 1 or 2 instead of xor.
Using bitwise OR instead of AND.
✗ Incorrect
The rightmost set bit is found by ANDing xor with its two's complement (-xor).
3fill in blank
hardFix the error in the loop that separates elements into two groups based on the rightmost set bit.
DSA Go
for _, num := range arr { if num & [1] != 0 { res1 = res1 ^ num } else { res2 = res2 ^ num } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using xor instead of rightmostSetBit in the condition.
Using a constant 1 which may not isolate the correct bit.
✗ Incorrect
We use rightmostSetBit to check which group each number belongs to.
4fill in blank
hardFill both blanks to initialize the result variables and print the two unique numbers.
DSA Go
res1, res2 := [1], [2] // After processing fmt.Println("Two unique numbers are:", res1, res2)
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with xor or -1 which causes wrong results.
Using 1 which changes the XOR outcome.
✗ Incorrect
Initialize res1 and res2 to 0 before XORing elements into them.
5fill in blank
hardFill all three blanks to complete the function that returns the two unique numbers.
DSA Go
func findTwoUnique(arr []int) (int, int) {
xor := 0
for _, num := range arr {
xor = xor [1] num
}
rightmostSetBit := xor & -[2]
res1, res2 := [3], 0
for _, num := range arr {
if num & rightmostSetBit != 0 {
res1 = res1 ^ num
} else {
res2 = res2 ^ num
}
}
return res1, res2
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of XOR.
Using constants instead of xor variable for two's complement.
Not initializing res1 to zero.
✗ Incorrect
Use XOR (^) to combine elements, xor variable for two's complement, and initialize res1 to 0.
