Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the number of subsets of {1, 2, ..., 8} whose elements sum to a multiple of 8? Run the code.
Problem: What is the number of subsets of {1, 2, ..., 8} whose elements sum to a multiple of 8? Run the code.
print(sum(1 for m in range(1 << 8) if sum(i + 1 for i in range(8) if m >> i & 1) % 8 == 0))What is printed?
- A. 31
- B. 32
- C. 6
- D. 256
Correct answer: B. 32
All 256 subsets including the empty one are tested, and 32 have sum ≡ 0 (mod 8). Leaving out the empty set gives 31, requiring sum exactly 8 gives 6, and 256 counts every subset.
More Coding Interview Problems questions
- Problem: How many pairs (i < j) of [1, 4, 9, 16, 25] have a[j] - a[i] divisible by 3? Run the code. python a = [1, 4, 9, 16, 25]…
- Problem: What is the number of set bits in 2^30 - 1? Run the code. python print(bin(2 30 - 1).count('1')) What is printed?
- Problem: How many integers in [1, 300] are multiples of 6 but not of 9? Run the code. python print(sum(1 for x in range(1, 301) if x % 6…
- Problem: How many ordered triples (a, b, c) of positive integers satisfy a + b + c = 7? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest possible value of gcd(a, b) when a + b = 60 and a != b with a, b positive? Run the code. python from math…
- Problem: What is the complexity of a single query in a sparse-table range minimum structure?