Data Structures & AlgorithmsModerateAmazon · Microsoft OA
Problem: Count the integers in [1, 1000] that are coprime to 1000.
Problem: Count the integers in [1, 1000] that are coprime to 1000.
from math import gcd print(sum(1 for i in range(1, 1001) if gcd(i, 1000) == 1))What is printed?
- A. 500
- B. 100
- C. 400
- D. 600
Correct answer: C. 400
1000 = 2³ · 5³, so φ(1000) = 1000 × (1 − 1/2) × (1 − 1/5) = 400. Removing only the even numbers (500) forgets the odd multiples of 5.
More Coding Interview Problems questions
- Reservoir sampling keeps k = 2 items from a stream of n = 10 items: the first 2 fill the reservoir, and item i (i > 2) is admitted with…
- Problem: An LFU cache (capacity 2) evicts the least frequently used key; ties are broken by least recently used. get returns −1 on a miss…
- Edge case: A generator expression is consumed twice. python g = (x x for x in range(4)) print(sum(g), sum(g)) What is printed?
- Problem: Sort students by grade (descending), breaking ties by name (ascending), using two passes of Python's STABLE sort. python s =…
- Problem: Find the length of the SHORTEST subarray with sum ≥ k when values may be negative (monotonic deque over prefix sums). python from…
- Problem: Longest subarray whose max − min ≤ limit, using two monotonic deques. python from collections import deque def longest(a, lim)…