Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the longest run of consecutive integers that all have at least one divisor in common with 30 (gcd greater than 1), starting from 2? Run the code.
Problem: What is the longest run of consecutive integers that all have at least one divisor in common with 30 (gcd greater than 1), starting from 2? Run the code.
from math import gcd
best = cur = 0
for x in range(2, 200):
cur = cur + 1 if gcd(x, 30) > 1 else 0
best = max(best, cur)
print(best)What is printed?- A. 6
- B. 4
- C. 5
- D. 7
Correct answer: C. 5
The run 2 to 6 (all sharing a factor with 30) is broken by the prime 7, and no later run is longer than 5.
More Coding Interview Problems questions
- Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code. python print(sum(1 for x in range(1, 501)…
- Problem: What is the sum of the first 12 terms of the sequence an = n^2 + 1 (n from 1 to 12)? Run the code. python print(sum(n n + 1 for n…
- Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code. python from…
- Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code. python print(sum(1 for x in…
- Problem: How many integer solutions (x, y) satisfy x^2 + y^2 = 65 with x, y >= 0? Run the code. python print(sum(1 for x in range(0, 9)…
- Problem: What is the maximum length of a strictly decreasing subsequence of [9, 4, 6, 3, 8, 1] ? Run the code. python a = [9, 4, 6, 3, 8…