Practice
Solution
Step 1: Understand set_bit calculation
Correct approach finds rightmost set bit usingxor_all & (-xor_all). Using OR instead breaks partitioning.Step 2: Consequence of bug
Incorrect set_bit causes wrong grouping of numbers, leading to incorrect missing numbers returned.Final Answer:
Option D -> Option DQuick Check:
Replacing OR with AND fixes partitioning and output [OK]
- Using OR instead of AND for set bit
- Misplacing XOR operations
- Returning wrong variables
Solution
Step 1: Identify complexity of outer and inner loops
The algorithm iterates once over the input array (n elements), performing constant time bitwise operations per element.Step 2: Check if recursion stack adds extra space
No recursion or extra data structures are used; only fixed variables 'ones' and 'twos' consume constant space.Final Answer:
Option D -> Option DQuick Check:
Linear time and constant space confirmed by single pass and fixed variables [OK]
- Assuming sorting or hash map space/time
- Confusing bit operations cost
Solution
Step 1: Analyze initialization of result
Result initialized to nums[0], then XORed again with nums[0] in loop, cancelling it out incorrectly.Step 2: Understand correct initialization
Result should start at 0 so XOR accumulates all elements exactly once.Final Answer:
Option A -> Option AQuick Check:
Initializing to zero avoids double XOR of first element [OK]
- Initializing with first element causes double XOR
- Loop includes first element again
- Misplaced return statement
Solution
Step 1: Understand two's complement representation
Negative numbers have sign-extended bits; masking with 0xFFFFFFFF treats them as unsigned 32-bit values.Step 2: Apply mask before counting bits
Masking ensures right shifts do not propagate sign bit and loop terminates correctly.Final Answer:
Option D -> Option DQuick Check:
Masking with 0xFFFFFFFF handles negative inputs correctly [OK]
- Using naive loop with signed shifts
- Taking absolute value loses bit pattern
- Left shift does not help counting bits
Solution
Step 1: Understand input constraints
Negative and zero inputs are not powers of two, so must be excluded explicitly.Step 2: Apply correct bit manipulation check
Check n > 0 first, then use n & (n - 1) == 0 to verify exactly one set bit.Step 3: Confirm correctness for 64-bit integers
This approach works efficiently for 64-bit integers as well, since bitwise ops are constant time.Final Answer:
Option A -> Option AQuick Check:
Explicit positive check avoids errors on zero/negative inputs [OK]
- Using incorrect bitwise operations
- Ignoring negative inputs
- Using floating point logs causing precision errors
