Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code.
Problem: How many integers in [1, 200] have an even number of 1-bits in their binary form? Run the code.
print(sum(1 for x in range(1, 201) if bin(x).count('1') % 2 == 0))What is printed?- A. 101
- B. 99
- C. 100
- D. 28
Correct answer: B. 99
Of 1..200, 99 have even popcount and 101 odd. 101 swaps the parity, 100 assumes an exact half (or counts 0), and 28 counts numbers with exactly two 1-bits.
More Coding Interview Problems questions
- Problem: What is the length of the longest subarray of [1, 1, 0, 1, 1, 1, 0, 1] containing at most one zero? Run the sliding-window code…
- Problem: How many distinct products a b arise when a and b are each a face value from 1 to 6? Run the code. python print(len({a b for a in…
- Problem: How many subsets of {1, 2, ..., 6} have a product that is a perfect square? Run the code (the empty set has product 1). python…
- Problem: What is the number of distinct values of floor(n / 2) + floor(n / 3) for 1 <= n <= 50? Run the code. python print(len({n // 2 + n…
- Problem: What is the largest sum of a subarray of length at least 2 in [4, -1, 5, -6, 3, 2]? Run the code. python a = [4, -1, 5, -6, 3, 2]…
- Problem: How many paths of length exactly 4 go from vertex 0 to vertex 0 in the complete graph K3? Run the code (walks allowed to repeat…