Data Structures & AlgorithmsVery hardTCS CodeVita · Microsoft SDE OA
The recursive extended Euclidean algorithm eg(a, b) returns (g, x, y) with a·x + b·y = g, using eg(a, b) = (g, y, x − (a//b)·y) when b ≠ 0 and eg(a, 0) = (a, 1, 0). Run it on (240, 46) and print the x it returns.
The recursive extended Euclidean algorithm eg(a, b) returns (g, x, y) with a·x + b·y = g, using eg(a, b) = (g, y, x − (a//b)·y) when b ≠ 0 and eg(a, 0) = (a, 1, 0). Run it on (240, 46) and print the x it returns.
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)[1])What is printed?- A. 1
- B. 47
- C. 2
- D. -9
Correct answer: D. -9
The recursion returns a particular Bezout pair; swapping the arguments returns a different valid pair, and the y coefficient or the gcd itself is a different output. Only the exact recursion as written determines the printed value.
More Coding Interview Problems questions
- Matrix chain: matrices A1..A6 have dimension sequence p = [30, 35, 15, 5, 10, 20, 25] (Ai is p[i−1] × p[i]). What is the minimum number of…
- Game of two eggs and 100 floors: you have two identical eggs and must find the highest floor from which a dropped egg survives, using as…
- Array b = [9, 2, 7, 4, 1, 8, 3]. Count the pairs (i < j) with b[i] > 2·b[j] (a reverse pair). Run the brute-force code. python…
- For the array c = [1, 2, 3, 4], consider every non-empty subarray and its XOR value. How many subarrays have an XOR value that is strictly…
- For the string "character", find the length of its longest palindromic subsequence (characters need not be adjacent, but must keep their…
- Count the subarrays of w = [1, 3, 2, 5, 4, 4, 2] whose maximum minus minimum is at most 3. Run the brute-force count. python…