Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the number of distinct subsets sums obtainable from {2, 3, 7} (including the empty subset)? Run the code.
Problem: What is the number of distinct subsets sums obtainable from {2, 3, 7} (including the empty subset)? Run the code.
from itertools import combinations
a = [2, 3, 7]
print(len({sum(c) for r in range(len(a) + 1) for c in combinations(a, r)}))What is printed?- A. 7
- B. 8
- C. 13
- D. 6
Correct answer: B. 8
Sums 0, 2, 3, 5, 7, 9, 10, 12 are all different, so 8. Leaving out the empty sum gives 7, assuming every value 0..12 is reachable gives 13, and 6 counts only 1- and 2-element subsets.
More Coding Interview Problems questions
- Problem: What is the largest prime factor of 2^20 - 1? Run the code. python n = 2 20 - 1 f = 2 last = 1 while f f <= n: while n % f == 0…
- Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code. python print(sum(1 for x in range(1, 501) if x % 2 and…
- Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code. python a = [1, 3, 2, 5, 4, 6] print(sum(1 for i…
- Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code. python a = [1, -2…
- Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code. python from itertools import product…
- Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code…