Practice
1. Consider the following code that generates all permutations of 3 elements using bitmask DP with iterative tabulation. What is the content of
dp[7] after the loops finish?def generate_permutations_iterative(n):
dp = [[] for _ in range(1 << n)]
dp[0] = [[]]
for mask in range(1 << n):
for i in range(n):
if not (mask & (1 << i)):
for perm in dp[mask]:
dp[mask | (1 << i)].append(perm + [i])
return dp[(1 << n) - 1]
perms = generate_permutations_iterative(3)
print(perms)easy
Solution
Step 1: Trace dp[0] initialization
dp[0] starts with [[]], representing empty permutation.Step 2: Iteratively build dp for all masks
For each mask, append unused elements to existing permutations, eventually filling dp[7] with all permutations of [0,1,2].Final Answer:
Option A -> Option AQuick Check:
dp[7] contains all 3! = 6 permutations of [0,1,2] [OK]
Hint: dp[(1<
Common Mistakes:
- Confusing mask indexing leading to missing permutations
- Off-by-one errors in bitmask checks
2. What is the output of the following code when the input is 11?
def hammingWeight(n: int) -> int:
return bin(n).count('1')
print(hammingWeight(11))
easy
Solution
Step 1: Convert 11 to binary
11 in binary is 1011.Step 2: Count '1's in '1011'
There are three '1's in the string.Final Answer:
Option C -> Option CQuick Check:
bin(11) = '0b1011', count('1') = 3 [OK]
Hint: Count '1's in binary string representation [OK]
Common Mistakes:
- Counting bits incorrectly
- Confusing decimal and binary digits
3. Identify the bug in the following code snippet for bitwise AND of numbers range:
medium
Solution
Step 1: Analyze loop condition
The loop runs while right != left, which fails if right < left, causing infinite loop or incorrect behavior.Step 2: Correct condition
Changing condition to 'right > left' ensures loop terminates when right reaches or goes below left.Final Answer:
Option D -> Option DQuick Check:
Loop condition must prevent infinite loops and handle equality correctly [OK]
Hint: Loop condition must be 'right > left' to terminate properly [OK]
Common Mistakes:
- Using '!=' instead of '>' causes infinite loops
- Misunderstanding bit-clearing operation
- Returning wrong variable
4. The following code attempts to find the missing number using XOR. Which line contains the subtle bug that causes incorrect results when the missing number is n?
medium
Solution
Step 1: Analyze the loop range for XORing indices
The loop runs from 0 to n-1 (range(n)), missing the number n itself.Step 2: Understand impact on missing number detection
Since n is not XORed, if n is the missing number, it won't be accounted for, causing wrong output.Final Answer:
Option C -> Option CQuick Check:
Changing loop to range(n+1) fixes the bug [OK]
Hint: Loop must include n to XOR all indices correctly [OK]
Common Mistakes:
- Off-by-one in loop range
- Forgetting to XOR n
- Assuming initialization causes bug
5. What is the time complexity of generating an n-bit Gray code sequence using the optimal bitwise formula
[i ^ (i >> 1) for i in range(1 << n)]?medium
Solution
Step 1: Analyze the loop iteration count
The list comprehension iterates over all integers from 0 to 2^n - 1, so 2^n iterations.Step 2: Analyze work per iteration
Each iteration performs constant time bitwise operations (XOR and right shift), so O(1) per iteration.Final Answer:
Option D -> Option DQuick Check:
Total time is proportional to 2^n, not multiplied by n or factorial [OK]
Hint: Loop runs 2^n times with O(1) work each [OK]
Common Mistakes:
- Confusing n with 2^n and claiming O(n)
- Assuming bitwise operations cost O(n)
- Mistaking factorial complexity from brute force backtracking
