Data Structures & AlgorithmsModerateCodeforces Div2 B · bit operations
Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code.
Problem: How many bits must be flipped to convert A = 29 into B = 15? Run the code.
A, B = 29, 15
print(bin(A ^ B).count('1'))What is printed?- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
29 = 11101 and 15 = 01111, and their XOR 10010 has 2 set bits. Counting differing positions by subtraction gives the same answer in this example.
More Coding Interview Problems questions
- Problem: What is printed by the stack-based decoder for '3[a2[c]]'? It expands k[s] to s repeated k times. python def decode(s): stack =…
- Problem: What is the minimum number of swaps needed to sort the permutation [4, 3, 2, 1] (values 1..4 each once)? Use the cycle…
- Problem: What is floor(sqrt(2000)) found by binary search? Run the code. python lo, hi = 0, 2000 while lo < hi: mid = (lo + hi + 1) // 2…
- Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on…
- Problem: Which statement about the greedy algorithm for 'minimum number of lecture rooms' (interval partitioning) is correct?
- Problem: Which statement about bitmask DP for a Hamiltonian path problem on n = 20 vertices is correct in the standard sense?