Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many integers in [1, 100] are divisible by 7 or by 11? Run the code.
Problem: How many integers in [1, 100] are divisible by 7 or by 11? Run the code.
print(sum(1 for x in range(1, 101) if x % 7 == 0 or x % 11 == 0))What is printed?
- A. 23
- B. 22
- C. 21
- D. 1
Correct answer: B. 22
Inclusion–exclusion: 14 multiples of 7 + 9 multiples of 11 − 1 multiple of 77 = 22. Forgetting to subtract 77 gives 23, subtracting it twice gives 21, and testing 'and' instead of 'or' counts only 77 (1).
More Coding Interview Problems questions
- Problem: What is 2^100 mod 7? Run the code. python print(pow(2, 100, 7)) What is printed?
- Problem: What is the length of the shortest path from node 1 to node 5 in the undirected graph with edges (1,2), (2,3), (1,4), (4,5)…
- Problem: How many connected components does the graph on vertices 0..5 with edges (0,1), (2,3), (4,5), (1,2) have? Run the union-find…
- Problem: Does the undirected graph with edges (0,1), (1,2), (2,0) contain a cycle? Run the union-find check and print 'cycle' or 'no…
- Problem: How many nodes are reachable from 0 in the directed graph with edges 0->1, 1->2, 3->0? Run the DFS code. python g = {0: [1], 1…
- Problem: Why does BFS find shortest paths in an unweighted graph in O(V + E) time?