Bellow is the question they asked me on the phone interview. This was part 1 of the question, where I've included the code that I wrote (correct or incorrect, this was the solution for part1 that I arrived at) Part 2 was to improve the original solution, the interviewer didn't really specify, as I guess I beat her to the punch when I said "you could probably make this better by storing previously seen values in a map", where she asked me to move down that route.
We ran out of time before I realized the correct solution of part 2, so I didn't include my partial solution below. There is a good veritasium video on this problem on YouTube that I suggest you take a look at. I was surprised when this question showed up, as I had watched that video recently and found it quite interesting.
As for how I got the interview; a Bloomberg recruiter reached out to me directly, randomly, and without prompt. I took the interview as more of a test to see how I would fair when I re-start my job search, as I am currently only 10 months into my current position.
Some notes and friendly reminders to myself and any others. I should have definitely done more research into Bloomberg, even though they are a behemoth, it's important to know the ins and outs of a company that you are truly interested in working for. I also rushed into part 2, and tripped over myself, which caused me to get confused, and I believe this contributed to my rejection in part with running out of time, as otherwise the question is pretty simple.
The Collatz Conjecture says if you take a positive integer N and repeatedly set either N=N/2 (if it's even) or N=3N+1 (if it's odd), N will eventually be 1.
5 -> 16 -> 8 -> 4 -> 2 -> 1 (5 steps).
Given N, how many steps does it take to reach 1?
const collatz = (num) => {
var steps = 0;
while (num !== 1 && num > 0) {
if (num % 2 === 0) {
num = num/2;
steps++;
} else if (num%2 !== 0){
num = 3*num +1;
steps++;
}
}
return steps;
}