Practice
nums = [1, 2, 3], what is the value of res after the function completes?Solution
Step 1: Trace backtrack calls and subsets added
Initially, empty subset [] is added. Then 1 is added, subsets with 1 are generated, then 2, then 3, backtracking after each.Step 2: Confirm order and completeness of subsets
Subsets are generated in lexicographic order due to sorting and incremental indices: [], [1], [1,2], [1,2,3], [1,3], [2], [2,3], [3].Final Answer:
Option A -> Option AQuick Check:
Matches expected subsets order from backtracking [OK]
- Confusing order of subsets or missing subsets
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
n elements using bitmask DP with iterative tabulation as shown in the code snippet? Assume appending to lists is O(1) amortized.Solution
Step 1: Identify number of states and transitions
There are 2^n bitmask states. Each state has up to n transitions to add an unused element.Step 2: Calculate total permutations generated
Total permutations are n!; each permutation is built by adding elements one by one, so total operations ~ n * n!.Final Answer:
Option B -> Option BQuick Check:
Time complexity is O(n * n!) due to building all permutations incrementally [OK]
- Confusing 2^n states with n! permutations
- Ignoring the cost of building each permutation step
Solution
Step 1: Analyze the loop iterations
Each iteration divides n by 4, so the number of iterations is proportional to log base 4 of n.Step 2: Express complexity in Big-O notation
Since log base 4 of n is O(log n), the time complexity is O(log n).Final Answer:
Option D -> Option DQuick Check:
Loop runs ~log4(n) times, which is O(log n) [OK]
- Confusing division steps as O(1)
- Mistaking log log n complexity
Solution
Step 1: Understand negative number representation
Negative numbers use two's complement, so their bit patterns differ from unsigned integers.Step 2: Convert to unsigned 32-bit representation
By masking with 0xFFFFFFFF, negative numbers become unsigned 32-bit integers, allowing consistent trie insertion and XOR calculation.Step 3: Avoid using absolute values or larger bit widths
Absolute values lose sign info; larger bit widths are unnecessary and inefficient.Final Answer:
Option B -> Option BQuick Check:
Unsigned 32-bit masking ensures correct bitwise trie operations [OK]
- Ignoring sign bits
- Using absolute values
- Assuming 64-bit needed
