Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many numbers in [1, 1000] are divisible by 3 and whose digit sum is also divisible by 3? Run the code.
Problem: How many numbers in [1, 1000] are divisible by 3 and whose digit sum is also divisible by 3? Run the code.
print(sum(1 for x in range(1, 1001) if x % 3 == 0 and sum(map(int, str(x))) % 3 == 0))What is printed?
- A. 111
- B. 333
- C. 667
- D. 37
Correct answer: B. 333
Divisibility by 3 already implies the digit-sum condition, so the answer is 1000 // 3 = 333. Treating the conditions as independent or as divisibility by 9 gives 111, 667 counts the complement, and 37 counts multiples of 27.
More Coding Interview Problems questions
- Problem: What is the time complexity of finding all prime factors of n up to 10^12 by trial division?
- Problem: Which problem is solved by the 'meet in the middle' technique on a subset-sum instance with n = 40?
- Problem: What does the 'two-phase' Kosaraju algorithm compute?
- Problem: Which best describes the complexity of Dijkstra on a graph with V = 10^5 and E = 10^6 using an array instead of a heap?
- Problem: What is the Stern-Brocot tree's relevance to competitive programming?
- Problem: Which algorithm finds all integer points on the convex hull boundary of a set of points?