Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10?
Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10?
print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in range(6) if m >> i & 1) == 10))What is printed?
- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
Each bitmask selects one subset; counting those whose element sum is 10 gives the answer.
More Coding Interview Problems questions
- Problem: What is the sum of the decimal digits of 9^9? python print(sum(int(c) for c in str(9 9))) What is printed?
- Problem: What is the length of the longest increasing subsequence of [3, 10, 2, 1, 20]? python import bisect a = [3, 10, 2, 1, 20] tails =…
- Problem: How many distinct values does floor(30 / i) take for i = 1 to 30? python print(len({30 // i for i in range(1, 31)})) What is…
- Problem: How many steps does Euclid's algorithm take on (2024, 748) before the remainder becomes 0? python a, b = 2024, 748 steps = 0…
- Problem: What is the sum of the divisors of 28 (a perfect number)? python print(sum(d for d in range(1, 29) if 28 % d == 0)) What is…
- Problem: How many pairs (i < j) of [1, 2, 3, 4, 5, 7] have an odd product? python a = [1, 2, 3, 4, 5, 7] print(sum(1 for i in…