FizzBuzz | InterviewBit | LeetCode | Solution in Python3, C++

FizzBuzz is a simple Interview problem. I present the solutions in Python3 and C++. IB Problem Link. LC Problem Link.

class Solution:
def fizzBuzz(self, A):
        ans = []
        for x in range(1, A+1):
            temp = ""
            if x % 3 == 0: temp += "Fizz"
            if x % 5 == 0: temp += "Buzz"
            if not len(temp): temp = str(x)
            ans.append(temp)
        return ans
vector<string> Solution::fizzBuzz(int A) {
    vector <string> ans;
    for (int i = 1; i <= A; i++) {
        string temp = "";

        if (i % 3 == 0) temp += "Fizz";
        if (i % 5 == 0) temp += "Buzz";
        if (!temp.length()) temp = to_string(i);

        ans.push_back(temp);
    }

    return ans;
}
Avatar for Tanishq Chaudhary

Producing high-quality intuitive explanations of interview problems. Currently covering LeetCode and InterviewBit.

    Comments

    1. Please write the proof.

    2. This solution need an amendment for 2 digit number.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.