Data Structures & AlgorithmsModerateTCS NQT · Product Company OA
Problem: What is the total number of 1-bits in the binary representations of 1 through 10? Run the code.
Problem: What is the total number of 1-bits in the binary representations of 1 through 10? Run the code.
print(sum(bin(i).count('1') for i in range(1, 11)))What is printed?- A. 15
- B. 19
- C. 21
- D. 17
Correct answer: D. 17
Popcounts of 1..10 are 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, which add up to 17. Stopping at 9 (dropping 10 = 1010, two bits) gives the tempting 15; range(1, 11) includes 10.
More Coding Interview Problems questions
- Problem: What is the minimum number of coins from {1, 2, 5} that sum to 11? Run the DP. python INF = 10 9 d = [0] + [INF] 11 for v in…
- Problem: How many binary strings of length 3 contain an even number of 1s? Run the code. python print(sum(1 for t in range(8) if…
- Problem: What is the sum of a[i] XOR a[j] over all pairs i < j of [1, 2, 3, 4]? Run the code. python a = [1, 2, 3, 4] print(sum(a[i] ^…
- Problem: How many distinct paths lead from vertex 0 to vertex 3 in the DAG with edges 0->1, 0->2, 1->2, 1->3, 2->3? Run the DP over the…
- Problem: What is the smallest integer x >= 100 divisible by 13? Run the code. python print(next(x for x in range(100, 200) if x % 13 ==…
- Problem: How many distinct non-empty prefixes does the string 'banana' have? Run the code. python s = 'banana' print(len({s[:i] for i in…