I was asked this question during an online assessment (I'm a student looking for an SDE internship) and couldn't solve it in time (I was only given 5 minutes...). Do you guys think 5 minutes for this question is enough btw? I'm just kinda curious how good everyone else is.
Anyways, here's the question:
You are given two tuples of integers
(A, B)and(C, D).
There are two operations you can do:
(A + B, B)
(A, A + B)
Write a function that returnsTrueif(A, B)can be transformed into(C, D)using the two operations,Falseotherwise.
Example:
Input: A = 2, B = 3, C = 8, D = 11
Output: True
I'll put what I 'thought' I wrote during the assessment here (I'm only 70% sure). It seems to be working fine and I'm not sure why it wouldn't pass the tests. If you guys know what the problem is, or the correct solution, please let me know!
def func(A, B, C, D):
if A == C and B == D:
return True
if A > C or B > D:
return False
return func(A + B , B, C, D) or func(A, A + B, C, D)