In this post, we’ll go through a classic interview question for developers: How to solve the FizzBuzz problem.
You are asked to write a function that accepts an integer. The function must implement a loop from 1 to the integer that was passed into the function. In the loop the function must print the following to the command window:
- if the number is divisible by 3 then print “Fizz”
- if the number is divisible by 5 then print “Buzz”
- if the number is divisible by both 5 and 3 then print “FizzBuzz”
- otherwise just print the number itself
We apply only one of these rules in the loop. E.g. 15 is divisible by both so we only print “FizzBuzz”, not “fizz”, then “buzz” and finally “FizzBuzz”.
The problem is simple to solve. However, if you have never encountered it before then it might turn into a tricky one in a stressful interview situation, especially with someone watching what you’re doing. So make sure you remember the below guidelines so that you’ll have more time over for the more complex tasks.
The solution is based on the relatively rarely used % operator, called the modulus. It returns the remainder of a division: e.g. 5 % 2 = 1 since we can fit 2 twice into 5 and we have a remainder of 1. Likewise 15 % 3 = 0 because we can fit 3 into 15 five times with nothing remaining.
We’ll have a couple of if-statements in the loop to check for the modulus. There are couple of details to watch out for:
- Check first if the integer is divisible by 3 and 5
- Use else-if-statements for the other conditions so that only one conditional block is carried out, i.e. the first one that meets the condition(s)
Why check for divisibility by 3 and 5 before anything else? Say that we get to 15 in the loop. If our first condition is to only check for divisibility by 3 then we’ll print “fizz” and that’s not what we want.
Why use else-if statements? If we have 3 “normal” if-statements then all of them will be evaluated and we don’t want that either. Again, taking 15 as an example, it is divisible by 3 which will print “Fizz”, it is also divisible by 5 which will print “Buzz” and then we also print “FizzBuzz” since 15 also fulfils the third condition.
Here’s the solution in C# code, but it can easily be ported to other popular OOP languages:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FizzBuzzSolution
{
public class FizzBuzz
{
public void RunFizzBuzz(int n)
{
for (int i = 1; i <= n; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if (i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
}
Do you have better solution? Please share it in the comments!
Not necessarily *better*, but an approach that exercises some pattern matching might be something like:
ReplyDeleteEnumerable.Range(1, n).ToList().ForEach(i =>
{
var whatToPrint = (i%3, i%5) switch
{
(0, 0) => "FizzBuzz",
(0, _) => "Fizz",
(_, 0) => "Buzz",
_ => i.ToString()
};
Console.WriteLine(whatToPrint);
});