Data Structures & AlgorithmsModerateCodeforces Div2 B · bit tricks
Problem: What does the following print?
Problem: What does the following print?
print(bin(12 & -12), 12 & -12)
- A. 0b1100 12
- B. 0b11 3
- C. 0b1 1
- D. 0b100 4
Correct answer: D. 0b100 4
The expression x & -x isolates the lowest set bit, which for 12 = 0b1100 is 0b100 = 4. Thinking it returns 12 or the bit index are common mistakes.
More Coding Interview Problems questions
- Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest…
- Problem: What does the following print? python def ed(a, b): m, n = len(a), len(b) dp = [[0] (n + 1) for in range(m + 1)] for i in range(m…
- Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?
- Problem: What does the following print? python a = list(range(10)) print(a[::-3], a[2:8:4])
- Problem: Count subarrays whose sum is divisible by k=5 for a = [4, 5, 0, -2, -3, 1]. python from collections import Counter def f(a, k): c…
- Problem: Kadane's algorithm is started with best = 0 and a running sum initialised to 0, then applied to an array where every number is…