Data Structures & AlgorithmsHardGoldman Sachs OA · LeetCode medium-hard

Problem: Extended Euclid returns g = gcd(a, b) and integers x, y with a·x + b·y = g.

Problem: Extended Euclid returns g = gcd(a, b) and integers x, y with a·x + b·y = g.
def eg(a, b):
    if b == 0: return a, 1, 0
    g, x, y = eg(b, a % b)
    return g, y, x - (a // b) * y
print(*eg(240, 46))
What is printed?
  1. A. 2 14 -73
  2. B. 2 23 -120
  3. C. 2 -9 47
  4. D. 2 -9 46

Correct answer: C. 2 -9 47

Back-substitution gives 240·(−9) + 46·47 = −2160 + 2162 = 2. Other Bézout pairs exist (x + 23k, y − 120k, e.g. 14 and −73), but the recursion returns this particular one.

More Coding Interview Problems questions

Exams that ask this topic