Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the total number of set bits in the binary representations of 1 through 100?
Problem: What is the total number of set bits in the binary representations of 1 through 100?
print(sum(bin(i).count('1') for i in range(1, 101)))What is printed?- A. 316
- B. 319
- C. 350
- D. 448
Correct answer: B. 319
Summing popcounts over 1..100 gives 319. Using range(1, 100) drops 100 = 1100100 (3 bits) and gives 316, assuming each of 7 bit positions is set half the time gives 100 × 7 / 2 = 350, and the exact formula 7 × 2⁶ = 448 holds only for 0..127.
More Coding Interview Problems questions
- Problem: What is the minimum number of jumps needed to reach the last index of [1, 2, 1, 1, 1] when a[i] is the maximum jump length from…
- Problem: What is the number of trailing zeros of 1000!? python n = 1000 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c) What is…
- Problem: How many 1-bits are set in 2^20 - 1? python print(bin(2 20 - 1).count('1')) What is printed?
- Problem: What is the maximum sum of a contiguous subarray of exactly length 3 in [5, -2, 7, 1, 0, 4]? python a = [5, -2, 7, 1, 0, 4]…
- Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers? python from itertools import combinations print(sum(1…
- Problem: How many integers in [1, 1000] are perfect squares or perfect cubes? python print(sum(1 for x in range(1, 1001) if any(k k == x…