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?- A. 2 14 -73
- B. 2 23 -120
- C. 2 -9 47
- 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
- Problem: A segment tree with lazy propagation supports 'add v to a[l..r]' and 'sum of a[l..r]' on an array of 8 zeros. python N = 8 t, lz…
- Problem: Count inversions (i < j, a[i] > a[j]) with a Fenwick tree over values, scanning from the right. python def inversions(a): n =…
- Problem: Return the longest word that can be built one letter at a time from other words in the list (every prefix must be present); ties…
- Problem: s1[i] and s2[i] are declared equivalent letters (the relation is reflexive, symmetric and transitive). Replace every letter of…
- Problem: Find all roots that give a tree of minimum height, by repeatedly peeling off leaves. python def mht(n, edges): adj = [set() for…
- Problem: Build a Huffman code for symbol frequencies and report the total number of bits for the encoded message (Σ freq × code length)…