Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many primes p <= 200 have p + 6 also prime? Run the code.
Problem: How many primes p <= 200 have p + 6 also prime? Run the code.
def isp(n):
return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(sum(1 for p in range(2, 201) if isp(p) and isp(p + 6)))What is printed?- A. 26
- B. 15
- C. 46
- D. 39
Correct answer: A. 26
Checking p and p + 6 for primes p ≤ 200 gives 26 pairs. Twin primes (p + 2) give 15, 46 counts all primes ≤ 200, and allowing p − 6 as well gives 39.
More Coding Interview Problems questions
- Problem: How many integers in [1, 2000] have an odd number of positive divisors? Run the code. python print(sum(1 for x in range(1, 2001)…
- Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code. python from itertools import combinations print(sum(1 for c…
- Problem: What is the largest k such that 2^k divides 12!? Run the code. python n = 12 c = 0 p = 2 while p <= n: c += n // p p = 2 print(c)…
- Problem: What is the length of the longest common prefix of 'interview', 'internet' and 'interval'? Run the code. python import os…
- Problem: How many subsets of {1, ..., 7} contain no two numbers that differ by exactly 1? Run the code. python print(sum(1 for m in…
- Problem: What is the minimum number of moves to sort [4, 1, 3, 2] when one move takes any element to the front? Run the code (answer = n…