I need some help in figuring out what I did wrong with this problem.
Question:
Given binary string representation of a positive integer find how many operations would it take to convert that number to 0. The operation can either be dividing the number by 2 if the number is even or subtracting 1 if the number is odd.
My code:-
def findNoOfOperations(s):
num = int(s, 2)
noOfOperations = 0
while num:
if num%2:
num= num-1
else:
num//=2
noOfOperations+=1
return noOfOperations;
print(findNoOfOperations("10") == 2)
print(findNoOfOperations("1111") == 7)I mentioned the time complexity as O(number of bits needed to represent the number) + O(logarithm of the integer value to the base 2) which finally boils down to O(logn), where n is the integer value(Correct me if I am wrong, as number of bits needed to represent is logn). The recruiter sent me an e-mail mentioning that I was rejected quoting, the solution I gave was suboptimal. It was an online assessment, and nowhere it was mentioned that I couldn't convert the binary string to integer value. How would you people approach this? What have I done wrong here?