Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code.
Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code.
print(sum(1 for x in range(1, 501) if (x % 4 == 0 or x % 6 == 0) and x % 12 != 0))What is printed?
- A. 167
- B. 208
- C. 85
- D. 126
Correct answer: D. 126
Multiples of 4 or 6 (lcm 12): 125 + 83 − 41 = 167; removing the 41 multiples of 12 leaves 126. Stopping at the union gives 167, adding without inclusion-exclusion gives 208, and subtracting the 12s twice gives 85.
More Coding Interview Problems questions
- Problem: What is the sum of the first 12 terms of the sequence an = n^2 + 1 (n from 1 to 12)? Run the code. python print(sum(n n + 1 for n…
- Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code. python from…
- Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code. python print(sum(1 for x in…
- Problem: How many integer solutions (x, y) satisfy x^2 + y^2 = 65 with x, y >= 0? Run the code. python print(sum(1 for x in range(0, 9)…
- Problem: What is the maximum length of a strictly decreasing subsequence of [9, 4, 6, 3, 8, 1] ? Run the code. python a = [9, 4, 6, 3, 8…
- Problem: How many numbers in [1, 1000] are divisible by 3 and whose digit sum is also divisible by 3? Run the code. python print(sum(1 for…