Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code.
Problem: How many integers in [1, 1000] are divisible by 2 and 5 but not by 4? Run the code.
print(sum(1 for x in range(1, 1001) if x % 2 == 0 and x % 5 == 0 and x % 4 != 0))What is printed?
- A. 50
- B. 100
- C. 75
- D. 150
Correct answer: A. 50
Divisible by 2 and 5 means a multiple of 10 (100 of them); removing the 50 multiples of 20 leaves 50. 100 ignores the 'not by 4' part, subtracting multiples of 40 gives 75, and dropping the 'by 2' condition gives 200 − 50 = 150.
More Coding Interview Problems questions
- Problem: Which algorithm finds the convex hull of n points in O(n log n) by sorting and building lower and upper chains?
- Problem: What is the expected linear-time algorithm for the smallest enclosing circle of n points?
- Problem: Which data structure answers range-GCD queries with point updates in O(log n) each?
- Problem: What is the complexity of computing the n-th Fibonacci number modulo a prime p using matrix exponentiation?
- Problem: Which technique counts pairs in an array whose XOR is less than K in O(n bits) time?
- Problem: What is the time complexity of the Wagner-Fischer dynamic programme for edit distance on strings of length n and m?