Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code.
Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code.
print(sum(1 for x in range(1, 1001) if x % 4 == 0 and x % 9 == 0))What is printed?
- A. 27
- B. 250
- C. 111
- D. 334
Correct answer: A. 27
4 and 9 are coprime, so both means a multiple of 36: 36 · 27 = 972 ≤ 1000, giving 27. 250 counts only multiples of 4, 111 only multiples of 9, and 334 counts 4 or 9 (the union).
More Coding Interview Problems questions
- Problem: What is the sum of the decimal digits of 2^15? Run the code. python print(sum(int(c) for c in str(2 15))) What is printed?
- Problem: How many integers in [1, 100] are not perfect squares? Run the code. python print(sum(1 for x in range(1, 101) if int(round(x…
- Problem: How many inversions does the array [5, 1, 4, 2, 3] have? Run the code. python a = [5, 1, 4, 2, 3] print(sum(1 for i in…
- Problem: What is the greatest common divisor of 12, 18, 30 and 42? Run the code. python from math import gcd from functools import reduce…
- Problem: What is the worst-case time complexity of binary search for the first occurrence of a value in a sorted array that contains…
- Problem: Which algorithm constructs an Euler circuit of a connected graph whose vertices all have even degree in linear time?