Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 200] are divisible by 2, 3 or 5?
Problem: How many integers in [1, 200] are divisible by 2, 3 or 5?
print(sum(1 for x in range(1, 201) if x % 2 == 0 or x % 3 == 0 or x % 5 == 0))What is printed?
- A. 206
- B. 146
- C. 140
- D. 145
Correct answer: B. 146
Inclusion–exclusion: 100 + 66 + 40 − (33 + 20 + 13) + 6 = 146. Just adding the three counts gives 206, subtracting the pairwise overlaps but forgetting to add back the 6 multiples of 30 gives 140, and range(1, 200) misses 200 itself (145).
More Coding Interview Problems questions
- Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10? python print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in…
- 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…