Data Structures & AlgorithmsModerateCodeforces Div2 A · bit operations
Problem: What does the following print?
Problem: What does the following print?
print(bin(10).count('1'), (10).bit_length())- A. 1 4
- B. 2 3
- C. 2 4
- D. 3 4
Correct answer: C. 2 4
10 is 1010 in binary, which has two set bits and bit length 4. Confusing bit_length with the highest set bit index (3) is the trap.
More Coding Interview Problems questions
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…
- What does this Python program print? python a = [2, -2, 3, -3, 1, 1] p = {0: 1}; s = 0; ans = 0 for x in a: s += x ans += p.get(s, 0) p[s]…
- What does this program print? python from collections import Counter a = [3, 1, 2, 3, 1, 2, 3] c = Counter(a) pairs = sum(v (v - 1) // 2…
- What does this program print? python a = [6, 3, 8, 1] mx = [0] len(a) mx[-1] = a[-1] for i in range(len(a) - 2, -1, -1): mx[i] = max(a[i]…
- Array a = [7, -3, 2, 5, -8, 4, 1, -1]. How many contiguous subarrays have a sum divisible by 3? (Zero is divisible by 3.)
- Sorted list [3, 6, 9, 12, 15, 18, 21]. How many index triples i < j < k satisfy vals[i] + vals[j] = vals[k]?