Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code.
Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code.
print(sum(1 for x in range(1, 1001) if sum(map(int, str(x))) == 5 and x % 5 == 0))What is printed?
- A. 21
- B. 6
- C. 7
- D. 1
Correct answer: C. 7
Digit sum 5 and ending in 0 or 5: 5, 50, 140, 230, 320, 410, 500, so 7. Ignoring divisibility gives 21, allowing only a final 0 gives 6, and allowing only a final 5 gives 1.
More Coding Interview Problems questions
- Problem: Which algorithm computes the number of shortest paths between all pairs in an unweighted graph in O(V E)?
- Problem: What is the time complexity of Kadane's algorithm extended to 2D (maximum-sum submatrix) with an n x n matrix?
- Problem: A hash table uses chaining, and every insertion first scans the key’s chain to make sure the key is not already present. What is…
- Problem: Which problem does the 'LCA by Euler tour and RMQ' reduce to?
- Problem: Which property does a 'matroid intersection' problem violate in general, making it harder than a single matroid?
- Problem: What is the time complexity of finding a minimum spanning tree in a complete graph with V vertices using Prim's algorithm and an…