Bloomberg | Phone | Collatz conjecture
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-08-05Added By Anonymous
Source: Link
Company: Bloomberg
Role: Senior Software Engineer
Interview Stage: Phone Interview
Location: unknown
Year: unknown
I interviewed for Bloomberg Senior Software Engineer role. The question was around Collatz conjecture. The main implementation was easy, they wanted me to write a function which returned the steps to reach 1.
Given a number, how many steps does it take to reach 1 given that:
* If the number is even, divide it by two.
* If the number is odd, triple it and add one.
However then they wanted me to save intermetiate results, for example:
I was explaining it might be good to use dynamic programming, since the steps to 5 are the [steps to 16] plus 1, but they do not wanted me to go in that direction.
Does anyone have a suggestion of how we can possible save the intermediate steps?
It looks impossible to me since we only know we need 5 steps until we have gone through the whole sequence.
EDIT:
I have found a solution. Keeping an intermediate array of the numbers, will allow us to populate the cache every time we calculate a new number, based on the lenght of the intermediate array. See code below (if you think about it, the numer of steps are "how many numbers" separate the the input number from 1, so for example in "5, 16, 8, 4, 2, 1" there are 5 numbers that separate 5 to 1, therefore we populate the cache like this: "cache[n] = len(seen) - i").
Role: Senior Software Engineer
Interview Stage: Phone Interview
Location: unknown
Year: unknown
I interviewed for Bloomberg Senior Software Engineer role. The question was around Collatz conjecture. The main implementation was easy, they wanted me to write a function which returned the steps to reach 1.
Given a number, how many steps does it take to reach 1 given that:
* If the number is even, divide it by two.
* If the number is odd, triple it and add one.
def count_steps(n):
if n<=0:
return -1
steps = 0
while n!=1:
n = 3*n+1 if n%2!=0 else n//2
steps+=1
return steps
I gave iterative and recusive solutions and also solution with caching results. However then they wanted me to save intermetiate results, for example:
Example N=5
5, 16, 8, 4, 2, 1 -> 5 steps
And they wanted me to save, not only the steps it takes to get 5 but also the steps to get 16, 8, 4,2 in the same function.I was explaining it might be good to use dynamic programming, since the steps to 5 are the [steps to 16] plus 1, but they do not wanted me to go in that direction.
Does anyone have a suggestion of how we can possible save the intermediate steps?
It looks impossible to me since we only know we need 5 steps until we have gone through the whole sequence.
EDIT:
I have found a solution. Keeping an intermediate array of the numbers, will allow us to populate the cache every time we calculate a new number, based on the lenght of the intermediate array. See code below (if you think about it, the numer of steps are "how many numbers" separate the the input number from 1, so for example in "5, 16, 8, 4, 2, 1" there are 5 numbers that separate 5 to 1, therefore we populate the cache like this: "cache[n] = len(seen) - i").
cache = {1: 0}
def calculate_steps(number):
if number < 1:
raise Exception('Number is not valid')
origianal_nr = number
seen = []
while number != 1:
if number in cache:
return cache[number]
seen.append(number)
if number % 2 == 0:
number = number // 2
else:
number = (number * 3) + 1
for i, n in enumerate(seen):
cache[n] = len(seen) - i
return cache[origianal_nr]