Practice
Solution
Step 1: Understand the problem constraints
The problem requires a sequence of 2^n numbers where each consecutive number differs by exactly one bit.Step 2: Identify the approach that guarantees correctness and efficiency
The reflected Gray code construction recursively builds the sequence by reflecting and prefixing bits, ensuring the one-bit difference property and covering all numbers exactly once.Final Answer:
Option B -> Option BQuick Check:
Reflected Gray code is the classic, proven method for this problem [OK]
- Greedy flipping bits can miss valid sequences or repeat numbers
- Brute force is correct but impractical for large n
- DP is not suitable as this is not a subproblem overlapping problem
remaining after processing the second byte in the input [197, 130, 1]?
def validUtf8(data):
remaining = 0
for byte in data:
if remaining == 0:
if (byte >> 5) == 0b110:
remaining = 1
elif (byte >> 4) == 0b1110:
remaining = 2
elif (byte >> 3) == 0b11110:
remaining = 3
elif (byte >> 7) == 0:
remaining = 0
else:
return False
else:
if (byte >> 6) != 0b10:
return False
remaining -= 1
return remaining == 0
Solution
Step 1: Trace first byte (197)
197 in binary is 11000101. (197 >> 5) == 0b110, so remaining is set to 1.Step 2: Trace second byte (130)
Since remaining is 1, check if (130 >> 6) == 0b10. 130 in binary is 10000010, so condition holds and remaining is decremented to 0.Final Answer:
Option C -> Option CQuick Check:
Remaining after second byte is 1 -> decremented to 0 after processing second byte [OK]
- Off-by-one decrement of remaining
- Misinterpreting bit shifts for continuation bytes
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
n elements but now elements can be reused any number of times (i.e., permutations with repetition). Which modification to the bitmask DP approach is correct?Solution
Step 1: Understand reuse means no visited tracking needed
Since elements can be reused, the visited bitmask is no longer meaningful.Step 2: Use DP over sequence length only
Iterative DP over length with choices from all elements at each step correctly generates permutations with repetition.Final Answer:
Option A -> Option AQuick Check:
Bitmask DP is unnecessary when elements can be reused [OK]
- Trying to reuse bitmask DP without modification
- Allowing bits to be set multiple times breaks bitmask logic
Solution
Step 1: Understand streaming constraints
In a stream, bytes arrive incrementally and must be validated on the fly without full buffering.Step 2: Adapt the continuation byte counter approach
Maintain the continuation byte counter state across bytes and validate each byte immediately, returning False if invalid.Step 3: Avoid buffering or recursion
Buffering or recursion delays validation and is not suitable for real-time streaming.Final Answer:
Option A -> Option AQuick Check:
Continuation counter with immediate validation fits streaming constraints [OK]
- Buffering entire characters breaks streaming
- Resetting counter incorrectly loses state
