Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many subsets of {1, 2, ..., 12} have an odd sum? Run the code.
Problem: How many subsets of {1, 2, ..., 12} have an odd sum? Run the code.
print(sum(1 for m in range(1 << 12) if sum(i + 1 for i in range(12) if m >> i & 1) % 2 == 1))What is printed?
- A. 4096
- B. 32
- C. 2048
- D. 64
Correct answer: C. 2048
The sum is odd iff an odd number of the 6 odd elements is chosen: 2⁵ ways for the odds times 2⁶ for the evens = 2048, half of all subsets. 4096 counts every subset, 32 forgets the evens, and 64 counts only the even elements' choices.
More Coding Interview Problems questions
- Problem: What is the largest value of x such that x^3 <= 10^12 (integer cube root)? Run the binary search. python lo, hi = 0, 10 6 while…
- Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code. python print(sum(1 for x in range(1, 1001) if…
- Problem: Which algorithm finds the convex hull of n points in O(n log n) by sorting and building lower and upper chains?
- Problem: What is the expected linear-time algorithm for the smallest enclosing circle of n points?
- Problem: Which data structure answers range-GCD queries with point updates in O(log n) each?
- Problem: What is the complexity of computing the n-th Fibonacci number modulo a prime p using matrix exponentiation?