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?- A. 1 4 1 3
- B. 1 -7 1 4
- C. 1 4 1 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
- Problem: Return the excel-style column title for a column number (1 → A, 26 → Z, 27 → AA). python def title(n): s = '' while n: n -= 1 s =…
- Problem: Return the number formed by the most significant digit of the sum of digits repeatedly added until one digit remains (digital…
- Problem: Count pairs (i < j) with a[i] > a[j] (inversions) in an array of n = 2 × 10⁵ values. Which approach runs within a 1-second limit?
- Problem: Answer q = 10⁵ queries 'sum of a[l..r]' on a static array of n = 10⁵ values. Which approach is best?
- Problem: An array of n = 10⁵ values receives 10⁵ operations, each either 'add v to a[i]' or 'sum of a[l..r]'. Which data structure gives…
- Problem: n ≤ 18 cities, find the shortest route visiting every city exactly once (travelling salesman). Which approach fits?