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

Fizz Buzz LeetCode Question:

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 which are multiples of both three and five output “FizzBuzz”.


n = 15,


Algorith for the Problem

The standard solution or algorith 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
            elif isDivisibleBy3:
                # Divides by 3, add Fizz
            elif isDivisibleBy5:
                # Divides by 5, add Buzz
                # Not divisible by 3 or 5, add the number

        return answer

C++ Solution for Fizz Buzz Problem

class Solution {
    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;

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.

Share your love

Leave a Reply