Data Structures & AlgorithmsHardICPC regional · number theory
Problem: Which complexity does the sieve of Eratosthenes have for finding all primes up to n?
- A. O(n)
- B. O(n log n)
- C. O(n sqrt n)
- D. O(n log log n)
Correct answer: D. O(n log log n)
Each prime p marks about n/p multiples, and summing 1/p over primes up to n gives log log n, so the total is O(n log log n).
More Coding Interview Problems questions
- Problem: How many 3-digit numbers are palindromes? python print(sum(1 for x in range(100, 1000) if str(x) == str(x)[::-1])) What is printed?
- Problem: How many integers in [1, 200] are divisible by 2, 3 or 5? python print(sum(1 for x in range(1, 201) if x % 2 == 0 or x % 3 == 0…
- Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10? python print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in…
- Problem: What is the sum of the decimal digits of 9^9? python print(sum(int(c) for c in str(9 9))) What is printed?
- Problem: What is the length of the longest increasing subsequence of [3, 10, 2, 1, 20]? python import bisect a = [3, 10, 2, 1, 20] tails =…
- Problem: How many distinct values does floor(30 / i) take for i = 1 to 30? python print(len({30 // i for i in range(1, 31)})) What is…