Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code.
Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code.
print(sum(1 for x in range(1, 501) if x % 4 == 0 or x % 6 == 0))What is printed?
- A. 167
- B. 208
- C. 125
- D. 83
Correct answer: A. 167
Multiples of 4 number 125, of 6 number 83, and of 12 number 41; by inclusion-exclusion 125 + 83 - 41 = 167.
More Coding Interview Problems questions
- Problem: How many ways can [1, 2, 3, 4] be split into two non-empty contiguous parts with equal sums? Run the code. python a = [1, 2, 3…
- Problem: Which algorithm finds all articulation points (cut vertices) of an undirected graph in O(V + E) time using DFS low-link values?
- Problem: Which statement about Kosaraju's algorithm for strongly connected components is correct?
- Problem: Which statement about the Edmonds-Karp maximum-flow algorithm (BFS augmenting paths) is correct?
- Problem: A road map has junctions 0–6 and two-way roads with lengths: 0-1 (4), 0-2 (1), 2-1 (2), 1-3 (5), 2-3 (8), 3-4 (3), 2-5 (10), 4-6…
- Problem: Starting with 8 singleton sets {0}..{7}, perform these unions in order: (0,1), (2,3), (1,3), (4,5), (6,7), (5,7), (3,4). After…