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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz

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 ;)

1
2
3
4
5
6
for (number = 1; number <= 100; number++) {
  if(number % 3 == 0 && number % 5 == 0) console.log("fizzbuzz")
  else if(number % 3 == 0) console.log("fizz")
  else if(number % 5 == 0) console.log("buzz")
  else console.log(number)
}
1
2
3
4
5
for number in range(1, 101):
  if(number % 3 == 0 and number % 5 == 0): print('fizzbuzz')
  elif(number % 3 == 0): print('fizz')
  elif(number % 5 == 0): print('buzz')
  else: print(number)

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

1
2
3
4
5
6
7
8
for (number = 1; number <= 100; number++) {
  let modulo3 = number % 3 == 0
  let modulo5 = number % 5 == 0
  if(modulo3 && modulo5) console.log("fizzbuzz")
  else if(modulo3) console.log("fizz")
  else if(modulo5) console.log("buzz")
  else console.log(number)
}
1
2
3
4
5
6
7
for number in range(1, 101):
  modulo3 = number % 3 == 0
  modulo5 = number % 5 == 0
  if(modulo3 and modulo5): print('fizzbuzz')
  elif(modulo3): print('fizz')
  elif(modulo5): print('buzz')
  else: print(number)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
fun main (){
  for (i in 1..100) {
    var d = ""
    if(i % 3 == 0) { d+= "fizz" }
    if(i % 5 == 0) { d+= "buzz" }
    if(d == "" ) {
      println(i)
    } else {
      println(d)
    }
  }
}

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

1
2
3
4
5
6
7
for (number = 1; number <= 100; number++) {
  let outcome = ""
  if(number % 3 == 0) outcome += "fizz"
  if(number % 5 == 0) outcome += "buzz"
  if(outcome) console.log(outcome)
  else console.log(number)
}
1
2
3
4
5
6
for number in range(1, 101):
  outcome = ""
  if(number % 3 == 0) : outcome += 'fizz'
  if(number % 5 == 0) : outcome += 'buzz'
  if(outcome != "") : print(outcome)
  else: print(number)

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.