Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA Go
func reverseBits(num uint32) uint32 {
var result uint32 = [1]
// rest of the code
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result to 1 or num causes wrong output.
Using negative numbers is invalid for unsigned integers.
✗ Incorrect
We start with result as zero because we will build the reversed bits from scratch.
2fill in blank
mediumComplete the code to shift the result left by one bit before adding the next bit.
DSA Go
for i := 0; i < 32; i++ { result = result [1] 1 // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift instead of left shift.
Using addition or subtraction instead of shift.
✗ Incorrect
We shift result left by 1 to make space for the next bit.
3fill in blank
hardFix the error in extracting the least significant bit from num.
DSA Go
bit := num [1] 1 // rest of the code
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR or XOR instead of AND.
Using shift operators incorrectly.
✗ Incorrect
Using bitwise AND with 1 extracts the least significant bit.
4fill in blank
hardFill both blanks to shift result left and add bit, then shift num right.
DSA Go
for i := 0; i < 32; i++ { result = (result [1] 1) | bit num = num [2] 1 }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping shift directions.
Using AND instead of OR to add bit.
✗ Incorrect
Shift result left to add bit, then shift num right to process next bit.
5fill in blank
hardFill all three blanks to complete the reverseBits function.
DSA Go
func reverseBits(num uint32) uint32 {
var result uint32 = [1]
for i := 0; i < 32; i++ {
bit := num [2] 1
result = (result [3] 1) | bit
num = num >> 1
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Wrong initialization of result.
Using OR or XOR instead of AND to get bit.
Shifting result right instead of left.
✗ Incorrect
Initialize result to 0, extract bit with AND, shift result left before OR.
