Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum that is a multiple of 4? Run the code.
Problem: How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7, 8} have a sum that is a multiple of 4? Run the code.
from itertools import combinations a = list(range(1, 9)) print(sum(1 for r in range(1, 9) for c in combinations(a, r) if sum(c) % 4 == 0))What is printed?
- A. 64
- B. 127
- C. 63
- D. 18
Correct answer: C. 63
Enumeration finds 63 non-empty subsets with sum ≡ 0 (mod 4). Including the empty set gives 64, counting even sums gives 127, and checking only 4-element subsets gives 18.
More Coding Interview Problems questions
- Problem: What is the minimum number of operations to make the array [5, 1, 4] strictly increasing where one operation increments any…
- Problem: What is the number of integers in [1, 999] whose digit product is 0? Run the code. python print(sum(1 for x in range(1, 1000) if…
- Problem: How many distinct pairs (i < j) of [6, 10, 15, 21] have gcd(ai, aj) = 1? Run the code. python from math import gcd a = [6, 10…
- Problem: What is the smallest positive integer that is divisible by every integer from 1 to 12? Run the code. python from math import lcm…
- Problem: Which technique gives the number of distinct subarrays of an array of length n in O(n log n) time using a suffix structure?
- Problem: Which statement about the Knuth optimization for interval DP is correct when its monotonicity conditions hold?