Recently I have been giving few interviews and I have faced this problem twice or thrice I guess where an interviewer asks me this problem.
The essence of problem goes like this, the task is to print 1 to N (In most cases N is 100), where if number is divisible by 3 you would print fizz, if its divisible by 5 you would print buzz, if its divisible by both then print fizzbuzz.
Lets see output when N is let say 15:


The problem seems very simple in nature but it is an interesting problem to try when you are learning a new language. As I have been giving interviews in Javascript and python I would try to put of solution in both language.
PS: Here I am considering N to be 100 ;)




Above solution are correct, but if we see it from optimizing prospective then it is not optimize. We are doing same operation multiple times i.e. we are taking modulo of 3 and 5 twice, first in common case then again in else if cases.
First step towards optimizing it could be as follows




How above approach is more optimize then previous one can be said by, In previous approach lets say number is 5, then there would 3 modulo operation would be runnning while in the newer approach for any case there would just two modulo operation and we are storing two number.
I recently learned this from Arnav also known as championswimmer. He was talking about same at this video on the youtube channel of Coding Blocks where he optimize it even more then this.
In above video he showed on of the way to optimize it even better with two modulo and single storage the example he had shown was in kotlin and it goes as follows.


We can optimize our solution even better with above approach lets try that as well.




PS: There has been some newer development on optimizing code and the newer approach we took. I had a brief discussion with Vijay aka Punnysher and Anop aka manchurian/potato :D. There would be another update to this.