Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many integers in [1, 100] are divisible by neither 2 nor 3? Run the code.
Problem: How many integers in [1, 100] are divisible by neither 2 nor 3? Run the code.
print(sum(1 for x in range(1, 101) if x % 2 != 0 and x % 3 != 0))What is printed?
- A. 17
- B. 50
- C. 67
- D. 33
Correct answer: D. 33
Inclusion-exclusion: 100 − 50 − 33 + 16 = 33. Forgetting to add back the 16 multiples of 6 gives 17, removing only the evens gives 50, and 67 counts the numbers divisible by 2 or 3 instead.
More Coding Interview Problems questions
- Problem: How many ordered pairs (a, b) with 1 <= a, b <= 9 make a b a perfect square? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the time complexity of Johnson's all-pairs shortest path algorithm on a sparse graph with V vertices and E edges?
- Problem: Which problem asks for the minimum number of rectangles of size 1 x 2 needed to tile a board with some cells removed (domino…
- Problem: What does the Hall condition require for a bipartite graph to have a matching covering the left side?
- Problem: Which algorithm solves the single-source shortest path problem in O(E) time on a directed acyclic graph with arbitrary edge…
- Problem: Which statement about the Edmonds-Karp algorithm's number of augmentations is correct?