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?
  1. A. 6
  2. B. 4
  3. C. 5
  4. 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

Exams that ask this topic