Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code.
Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code.
print(sum(1 for x in range(1, 501) if x % 2 and x % 3 and x % 5))What is printed?
- A. 484
- B. 133
- C. 366
- D. 134
Correct answer: D. 134
Inclusion-exclusion gives 500 − 516 + 166 − 16 = 134 numbers coprime to 30. Using or instead of and gives 484, the density estimate 500 · 8/30 gives 133, and 366 counts the complement.
More Coding Interview Problems questions
- 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…
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…
- Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code. python print(sum(1 for x in range(1, 501) if…