Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Find the integer x (0 ≤ x < m) such that 3x ≡ 1 (mod 11) using the extended Euclidean algorithm.

Problem: Find the integer x (0 ≤ x < m) such that 3x ≡ 1 (mod 11) using the extended Euclidean algorithm.
def ext(a, b):
    if b == 0: return a, 1, 0
    g, x, y = ext(b, a % b)
    return g, y, x - (a // b) * y
g, x, _ = ext(3, 11)
print(g, x % 11, (3 * (x % 11)) % 11, pow(3, -1, 11))
What is printed?
  1. A. 1 4 1 3
  2. B. 1 -7 1 4
  3. C. 1 4 1 4
  4. D. 3 4 1 4

Correct answer: C. 1 4 1 4

ext returns x, y with 3x + 11y = 1; here x = 4 (3·4 = 12 ≡ 1). The raw x can be negative, so it is normalised with % 11. Python 3.8+ pow(a, −1, m) gives the same inverse. An inverse exists only when gcd(a, m) = 1. O(log m).

More Coding Interview Problems questions

Exams that ask this topic