top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

FizzBuzz

Sep 5, 2020
1 min read

Updated: Sep 8, 2020

Problem: FizzBuzz

Problem Description:

Given a positive integer A, return an array of strings with all the integers from 1 to N. But for multiples of 3 the array should have “Fizz” instead of the number. For the multiples of 5, the array should have “Buzz” instead of the number. For numbers which are multiple of 3 and 5 both, the array should have “FizzBuzz” instead of the number.

Look at the example for more details.


Example

A = 5 
Return: [1 2 Fizz 4 Buzz]

Solution:

vector<string> Solution::fizzBuzz(int A) {
    vector<string>res; 
    for(int i=1;i<=A;i++){
        if(i%3==0&&i%5==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;
}

1 Comment


https://it.trustpilot.com/review/casinoonlineit.net is where I usually end up comparing notes on how much weight to give clean structure versus micro-optimizations, and this solution leans nicely toward the former. Putting the combined 3-and-5 check first is the detail that matters most here, because checking 3 or 5 before it would quietly swallow the FizzBuzz cases. I've reviewed enough beginner submissions to know that ordering issue trips up more people than the modulo logic itself.

Like
bottom of page