How to Solve Fizz Buzz Interview Question in Right Way

Fizz Buzz is a very popular interview question that is asked a lot in software engineering interviews. So we will be looking into the solutions of Fizz Buzz using Python, C++.

Fizz Buzz Problem Statement:

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three, it should output “Fizz” instead of the number, and for the multiples of five output “Buzz”. For numbers that are multiples of both three and five output should be “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Algorithm To Follow for Fizz Buzz

The standard solution or algorithm for this problem is as followed.

  • Initialize an empty list named answer.
  • Start a Iteration from 1 to N.
  • For every number, if it is divisible by both 3 and 5 then add FizzBuzz to the list.
  • Else, check if number is divisible by 3, add Fizz to list.
  • Else, check if number is divisible by 5, add Buzz to list.
  • Else, add the number to the list.

Python Solution for Fizz Buzz Problem.

class Solution:
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        # ans list
        answer = []

        for num in range(1,n+1):

            isDivisibleBy3 = (num % 3 == 0)
            isDivisibleBy5 = (num % 5 == 0)

            if isDivisibleBy3 and isDivisibleBy5:
                # Divides by both 3 and 5, add FizzBuzz
                answer.append("FizzBuzz")
            elif isDivisibleBy3:
                # Divides by 3, add Fizz
                answer.append("Fizz")
            elif isDivisibleBy5:
                # Divides by 5, add Buzz
                answer.append("Buzz")
            else:
                # Not divisible by 3 or 5, add the number
                answer.append(str(num))

        return answer

As you can see in the above code, this is pretty much an easy solution if you follow the if-else conditions correctly. And with help of this question interview aims to verify your knowledge related to if-else conditions.

Because in general many candidates makes the mistake of using the if condition first to check if it is divisible by 3 and if the number is divisible by 5 and later divisible by both. And this is the wrong approach.

Let us see the solution for the Fizz Buzz problem in C++.

C++ Solution for Fizz Buzz Problem

// C++ implementation of the approach
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for(int i=1;i<=n;i++){
            if(i%15==0) res.push_back("FizzBuzz");
            else if(i%3==0) res.push_back("Fizz");
            else if(i%5==0) res.push_back("Buzz");
            else res.push_back(to_string(i));
        }
        return res;
    }
};
 
// Driver code to Print Fizz Buzz
int main()
{
    vector<string> ans;

    Solution newObj;

    ans = newObj.fizzBuzz(15);

    for(string x:ans)
        cout<<x<<endl;
     
    return 0;
}

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
How to Solve Fizz Buzz Interview Question in Right Way

Wrap Up

That is all for the FizzBuzz problem the common interview question asked for software engineering roles. Let me know if you have any better solution to the above problem in the comments section.

If you liked the answer then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Wanna read more interview-related questions? Check Top Interview Questions category.

Leave a Comment