the cde tlks Understanding the language of code as it speaks
The Fizz Buzz Case
The Fizz Buzz Case

A very popular question in the recent era of interviews and even which can stump up a fairly good programmer with respect to problem solving skills. So let's check out what the FizzBuzz problem is all about.

This was or one can say still is a game played among school children for them to be able to grasp or gain strong foothold in arithmetic operations like division and multiplications. In 2007, a person named as Imran Ghory thought of it as a way of using it in interviews to put candidates on spot and testing their skills.

So, what the Fizz Buzz problem actually talks about is. It says for a given number,

  • print fizz if the given number is divisible by 3.
  • print buzz if the given number is divisible by 5.
  • print fizzbuzz if the given number is divisible by both 5 and 3.
  • print the number if all the above conditions are not met.

So, for example if the given number is,

  • 9, we would print fizz.
  • 25, we would print buzz.
  • 30, we would print fizzbuzz.
  • 28, we would print 28

So, well there are lot of so's here isn't it?? Moving ahead how would we go to solve this problem at hand. Remember there is not a single way to solve any problem, there are many many ways. Generally, in interview the focus is on the approach rather than the final answer. 

So, uhhh.. not that word again.. we won't digress further.. Here are different ways we can do that starting from the simplest.

Approach I: Modulo Operations

if(number%3==0 && number%5==0)

     print fizzbuzz

else if(number%3==0)

    print fizz

else if(number%5==0)

    print buzz

else

   print number

In this approach, we are literally converting the conditions into code. 

Approach II: Modulo Operations Again

if(number%15==0)

     print fizzbuzz

else if(number%3==0)

    print fizz

else if(number%5==0)

    print buzz

else

   print number

The difference here from the first approach is we minimize one modulo check. 

Approach III: Concatenation

string output = ""

if(number%3==0)

    output="fizz";

if(number%5==0)

    output=output+"buzz"

if(output=="")

    output=number  

print output

Notice that, in this approach there is no mention of else keyword. What we are trying to do is basically concatenate the strings based on conditions and print the resulting output. The difference here from the first two approaches is we minimize one if and the extra modulo checks. 

In addition to above approaches, there are other approaches as well like using libraries like Itertools with python, using lamda expressions. Well, I believe we have atleast been able to provide a start for the curious mind to wander in search of multiple approaches.

There maybe a very tiny optimization or spike in processing that we might get however we need to understand that as the no of variables increase or in coding terms no. of threads increase this somewhat small bump in speed can have a considerable performance boost.

Well, that's about it for now.. Until next time keep finding what the code is talking about.

Powered by Froala Editor