Data Structures & AlgorithmsModerateICPC regional · bit manipulation
Problem: What does the following print?
Problem: What does the following print?
print([format(i ^ (i >> 1), '03b') for i in range(4)])
- A. ['000', '001', '010', '011']
- B. ['000', '010', '011', '001']
- C. ['000', '001', '011', '010']
- D. ['001', '000', '011', '010']
Correct answer: C. ['000', '001', '011', '010']
i XOR (i >> 1) is the reflected Gray code, so consecutive values differ in one bit: 000, 001, 011, 010. Printing plain binary is the trap.
More Coding Interview Problems questions
- Problem: Count the pairs (i < j) with a[i] + a[j] < 6 for the array [5, 1, 4, 2, 3]. python def count(a, T): a = sorted(a) i, j = 0…
- Problem: Mo's algorithm answers q range queries on an array of length n by sorting queries into blocks of size about sqrt(n) and moving…
- Problem: In a segment tree with lazy propagation, what is the cost of one range-add update on an array of length n?
- Problem: What does the following print? python print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
- Problem: What does the following print? python print(bin(12 & -12), 12 & -12)
- Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest…