Jump to content
IGNORED

Software Devs: Anyone ever been asked the FizzBuzz question on an interview? How did you solve it?


RH
 Share

Recommended Posts

Member · Posted

When I go into job interviews, I will study up a bit just to brush up on terminology but I don't really deep-dive into common Software Developer/Engineer questions.  There are to many, and I can write what I need to write.

I first ran into the "Fizz Buzz" question about 4 years ago.  If you are unfamiliar with it, you are usually given a whiteboard and told to write pseudo-code that meets the following requirements.

1. A user enters a number...

2. If it is divisible by 3, output a line with the word "Fizz".

3. If divisible by 5, output a line with the word "Buzz".

4. If the number is divisible by 3 AND 5, you must output "Fizz Buzz" on the same line. 

Supposedly, the point of this question is not to present any "gotchas" but to present a problem that can be solved multiple ways, and to see how the interviewee processes and solves problems.  I think it's an interesting question, and I had fun with it!  So how did you solve this problem, if you ran into it.  Or, if you've never heard of it (even though it's probably 6-8 years old, at least) how would you solve it today, without looking up solutions?

I'll post my solution a bit later.  My answer actually landed me the position at a major bank.  I was actually shocked that my answer wasn't a more obvious solution.

  • Like 1
Link to comment
Share on other sites

Administrator · Posted

It's never come up for me, though I've seen it in discussion numerous times online and solved it myself for "fun". 

If I was asked this question in an interview I'd be a cheeky fucker and say my solution is something along these lines:

  • Open terminal
  • Navigate to project folder
  • Run npm install
  • Run npm i fizzbuzz
  • Load up a js file with the following (from Github):
  • var fizzbuzz = require('fizzbuzz').fizzbuzz;
    for(var i=1;i<=100;i++){	console.log(String(fizzbuzz(i)));
    }

 

Ezpz.

  • Haha 1
Link to comment
Share on other sites

Member · Posted
1 minute ago, Gloves said:

It's never come up for me, though I've seen it in discussion numerous times online and solved it myself for "fun". 

If I was asked this question in an interview I'd be a cheeky fucker and say my solution is something along these lines:

  • Open terminal
  • Navigate to project folder
  • Run npm install
  • Run npm i fizzbuzz
  • Load up a js file with the following (from Github):
  • 
    var fizzbuzz = require('fizzbuzz').fizzbuzz;
    for(var i=1;i<=100;i++){	console.log(String(fizzbuzz(i)));
    }

 

Ezpz.

Me (Interviewer): Ok, cool.  You know how to import via NPM... but you are on a locked down network and cannot use NPM, and you have no such libraries at your disposal. 😉

Link to comment
Share on other sites

Administrator · Posted
2 minutes ago, RH said:

Me (Interviewer): Ok, cool.  You know how to import via NPM... but you are on a locked down network and cannot use NPM, and you have no such libraries at your disposal. 😉

I don't wanna work here, thanks for your time.

  • Haha 3
Link to comment
Share on other sites

21 minutes ago, RH said:

I'll post my solution a bit later.  My answer actually landed me the position at a major bank.  I was actually shocked that my answer wasn't a more obvious solution.

Doing a simple if statement block?

if (!(num % 3) && !(num % 5)) {
	console.log("Fizz Buzz")
} else if (!(num % 3)) {
  	console.log("Fizz")
} else if (!(num % 5)) {
  	console.log("Buzz")
} else {
  console.log("Neither Fizz nor Buzz")
}

PS: Sorry for the JS, I was just writing some so that's what I decided to use.

Link to comment
Share on other sites

Member · Posted
3 minutes ago, MachineCode said:

Doing a simple if statement block?


if (!(num % 3) && !(num % 5)) {
	console.log("Fizz Buzz")
} else if (!(num % 3)) {
  	console.log("Fizz")
} else if (!(num % 5)) {
  	console.log("Buzz")
} else {
  console.log("Neither Fizz nor Buzz")
}

PS: Sorry for the JS, I was just writing some so that's what I decided to use.

No problem with the JS.  Language doesn't matter.  My solution was quite similar to this.  Hint line #1 can be simplified. Alternatively (depending on the language you are using) you can drop the first if, and turn last two "if" checks to be straight if-blocks, and once you're done writing "Fizz" and/or "Buzz", just write a new line... but that wasn't my solution.

Link to comment
Share on other sites

I think i did have to answer it for one interview but i dont remember exactly, maybe 7 years ago.

The original purpose of the question was to have a potential candidate actually write code and build/execute it to prove they could.

IIRC it was created in response to receiving hundreds of applications from people who literally could not write even the most basic code.

Its not supposed to be tricky and its not looking for overly clever answers. Its low pressure on purpose. The only goal was to filter out people who had no business applying for a programming position. 

Of course nerds pick up on it and try to prove to each other how smart they which is why its gained so much traction online. And then who knows how it gets filtered through management types before winding up in an interview.

 

Link to comment
Share on other sites

21 minutes ago, RH said:

No problem with the JS.  Language doesn't matter.  My solution was quite similar to this.  Hint line #1 can be simplified. Alternatively (depending on the language you are using) you can drop the first if, and turn last two "if" checks to be straight if-blocks, and once you're done writing "Fizz" and/or "Buzz", just write a new line... but that wasn't my solution.

Now I'm curious as I can think of like 4 solutions to the problem that spring to mind immediately without any extra thought. Just had this one pop in a second ago.

let text = ""

if (!(num % 3)) {
  text += "Fizz "
}

if (!(num % 5)) {
  text += "Buzz"
}

console.log(text.trim())

 

Edited by MachineCode
Link to comment
Share on other sites

Member · Posted
1 minute ago, MachineCode said:

Now I'm curious as I can think of like 4 that spring to mind immediately without any extra thought. Just had this one pop in a second ago.


let text = ""

if (!(num % 3)) {
  text += "Fizz "
}

if (!(num % 5)) {
  text += "Buzz"
}

console.log(text.trim())

 

This is a rather solid one, and I think might be one of the simplest solutions without any special trickery.  Here was my solution that I came up with at the whiteboard when I first heard it.

 

while(true)
{
    var number = Console.ReadLine(); //Assume it is a number, no validation/conversions required.
    if (number % 15 == 0) Console.WriteLine("FizzBuzz");
    else if (number % 3 == 0) Console.WriteLine("Fizz");
    else if (number % 5 == 0) Console.WriteLine("Buzz");
  
  	Console.Write("Enter 'y' to stop? ");
    if (Console.ReadLine().ToLower() == "y") break;
}

I asked one of my interviewers why they chose me over many candidates once I was hired.  He specifically said that my answer to this question was what was the hook.  It was an interview with three people, and the guy who asked me this question specifically asked me about the mod 15 if-check.  3 and 5  are prime which means any number evenly divisible by 15 will meet the "FizzBuzz" criteria and, likewise, no other number would meet the full criteria for being divisible by BOTH numbers.  By using this mechanism, you only have to run one check operation to output the response.  However, this does mean that you have one additional check for every other number entered that is not divisible by 15.  So this probably isn't the most efficient answer.

Since this was an interview, I didn't have time to model which approach would be more efficient in the long term.  I'm sure its @MachineCodes response is the most efficient mechanism, however, the use of prime number mathematics as a fundamental aspect of my solution was enough for them to see that I, apparently, "think out of the box"...

I then went on to work about 14 months on a spaghetti mish-mash of code built into a WinForms application.  Ehhh, whatever. You do what you gotta do to pay the bills.

Link to comment
Share on other sites

10 minutes ago, RH said:

I then went on to work about 14 months on a spaghetti mish-mash of code built into a WinForms application.  Ehhh, whatever. You do what you gotta do to pay the bills.

HA! That's amazing. Hell I once took on a job where they required me to write in coldfusion (shudders). My prove it test (and I had never written a line of CF prior mind you) was to write an API connector that returned the data in XML format. My "test" went into their code base 3 months before they called me back with an offer. A stupidly low offer that I took due to being desperate for work. But ye dude, been there.

 

RE FizzBuzz: I thought of the mod 15 version as well but then realized it was only ever gonna be 1 line of output so I went with string concatenation while negating the and (or mod15) condition.

I did just realize a flaw in mine though. a value of 0 triggers all conditions so I have to wrap it all in a 0 check, add a && num !===0 to all, or just do the mod 15 version.

Edited by MachineCode
Link to comment
Share on other sites

1 hour ago, RH said:

This is a rather solid one, and I think might be one of the simplest solutions without any special trickery.  Here was my solution that I came up with at the whiteboard when I first heard it.

 


while(true)
{
    var number = Console.ReadLine(); //Assume it is a number, no validation/conversions required.
    if (number % 15 == 0) Console.WriteLine("FizzBuzz");
    else if (number % 3 == 0) Console.WriteLine("Fizz");
    else if (number % 5 == 0) Console.WriteLine("Buzz");
  
  	Console.Write("Enter 'y' to stop? ");
    if (Console.ReadLine().ToLower() == "y") break;
}

I asked one of my interviewers why they chose me over many candidates once I was hired.  He specifically said that my answer to this question was what was the hook.  It was an interview with three people, and the guy who asked me this question specifically asked me about the mod 15 if-check.  3 and 5  are prime which means any number evenly divisible by 15 will meet the "FizzBuzz" criteria and, likewise, no other number would meet the full criteria for being divisible by BOTH numbers.  By using this mechanism, you only have to run one check operation to output the response.  However, this does mean that you have one additional check for every other number entered that is not divisible by 15.  So this probably isn't the most efficient answer.

Since this was an interview, I didn't have time to model which approach would be more efficient in the long term.  I'm sure its @MachineCodes response is the most efficient mechanism, however, the use of prime number mathematics as a fundamental aspect of my solution was enough for them to see that I, apparently, "think out of the box"...

I then went on to work about 14 months on a spaghetti mish-mash of code built into a WinForms application.  Ehhh, whatever. You do what you gotta do to pay the bills.

I thought about incorporating 15 directly like you did, but I thought was would be considered inelegant.

That's what sucks about these problems - too subjective. What are you trying to accomplish? Code clarity? minimum opcodes? memory space?

I would have ventured something like (pardon my pseudocode)

x = (num mod 3)

y = (num mod 5)

z = x + y

if z < 2 then

{

if !(z)  then "Fizz Buzz"

else if (y) then "Fizz" else "Buzz"

}

My argument would be that the mod operator is time consuming, so limiting to 2 mods saves time overall. I believe string addition is costly too, so this avoids that.

 

 

Link to comment
Share on other sites

3 hours ago, RH said:

When I go into job interviews, I will study up a bit just to brush up on terminology but I don't really deep-dive into common Software Developer/Engineer questions.  There are to many, and I can write what I need to write.

I first ran into the "Fizz Buzz" question about 4 years ago.  If you are unfamiliar with it, you are usually given a whiteboard and told to write pseudo-code that meets the following requirements.

1. A user enters a number...

2. If it is divisible by 3, output a line with the word "Fizz".

3. If divisible by 5, output a line with the word "Buzz".

4. If the number is divisible by 3 AND 5, you must output "Fizz Buzz" on the same line. 

Supposedly, the point of this question is not to present any "gotchas" but to present a problem that can be solved multiple ways, and to see how the interviewee processes and solves problems.  I think it's an interesting question, and I had fun with it!  So how did you solve this problem, if you ran into it.  Or, if you've never heard of it (even though it's probably 6-8 years old, at least) how would you solve it today, without looking up solutions?

I'll post my solution a bit later.  My answer actually landed me the position at a major bank.  I was actually shocked that my answer wasn't a more obvious solution.

Never had to do fizzbuzz in an interview even though i've been in multiple technical interviews. Also never bothered to look it up. 

if(input mod 3 == 0) { 
  print("Fizz "); 
} 
if(input mod 5 == 0) { 
  print("Buzz"); 
} 

This is pseudocode and each print statement does not generate a new line. 

Edited by MiamiSlice
Link to comment
Share on other sites

2 hours ago, RH said:

This is a rather solid one, and I think might be one of the simplest solutions without any special trickery.  Here was my solution that I came up with at the whiteboard when I first heard it.


while(true)
{
    var number = Console.ReadLine(); //Assume it is a number, no validation/conversions required.
    if (number % 15 == 0) Console.WriteLine("FizzBuzz");
    else if (number % 3 == 0) Console.WriteLine("Fizz");
    else if (number % 5 == 0) Console.WriteLine("Buzz");
  
  	Console.Write("Enter 'y' to stop? ");
    if (Console.ReadLine().ToLower() == "y") break;
}

 

I'm surprised they hired you. The prompt says you have to output "Fizz Buzz" if it's divisible by 3 and 5, but you ouput "FizzBuzz." Tsk, tsk. 🙂

Link to comment
Share on other sites

Here's a really cheap way to do it with only two statements in an array structure starting at count 0:

deeznuts[15] = {"Fizz Buzz", "", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "" };

print( deeznuts [ num mod 15 ] );

Edited by Daniel_Doyce
Link to comment
Share on other sites

1 hour ago, Daniel_Doyce said:

I thought about incorporating 15 directly like you did, but I thought was would be considered inelegant.

That's what sucks about these problems - too subjective. What are you trying to accomplish? Code clarity? minimum opcodes? memory space?

I would have ventured something like (pardon my pseudocode)

x = (num mod 3)

y = (num mod 5)

z = x + y

if z < 2 then

{

if !(z)  then "Fizz Buzz"

else if (y) then "Fizz" else "Buzz"

}

My argument would be that the mod operator is time consuming, so limiting to 2 mods saves time overall. I believe string addition is costly too, so this avoids that.

 

 

The purpose is to get correct output with code that is not a trainwreck. Micro optimizing to eliminate mod ops is counterproductive for the original exercise. Your answer works but I'd be wary of your approach as it sacrifices clarity. 

 

24 minutes ago, Daniel_Doyce said:

Here's a really cheap way to do it with only two statements in an array structure starting at count 0:

deeznuts[15] = {"Fizz Buzz", "", "", "Fizz", "", "Buzz", "", "", "", "Fizz", "Buzz", "", "Fizz", "", "" };

print( deeznuts [ num mod 15 ] );

Ok, make it work for all ints 1 to 1000. Or scalable for any arbitrary range.

 

One missing requirement from the original problem was to print the integer if it wasnt any of the other cases. Its not much tougher but does require awareness of meeting none of the explicit conditions.

Edited by Lincoln
Link to comment
Share on other sites

Administrator · Posted
4 minutes ago, Lincoln said:

One missing requirement from the original problem was to print the integer if it wasnt any of the other cases. Its not much tougher but does require awareness of meeting none of the explicit conditions.

I've included this in my Vue implementation linked above.

It's a fun little exercise to tear apart honestly, even if doing so goes well beyond the scope of the originally intended use.

Link to comment
Share on other sites

5 minutes ago, Lincoln said:

The purpose is to get correct output with code that is not a trainwreck. Micro optimizing to eliminate mod ops is counterproductive for the original exercise. Your answer works but I'd be wary of your approach as it sacrifices clarity.

No purpose was stated other than producing the correct answer. I'd agree with you if clarity was a specified goal.

 

Ok, make it work for all ints 1 to 1000

Check the code again... It even works with numbers above 1000 too!

 

 

 

Link to comment
Share on other sites

Editorials Team · Posted

The obvious solution is to build each one within layers and layers of libraries, put everything on one line, embed as many different languages as possible within your code, and have the user's terminal go apeshit for ages when the thing is running.  Then you'll stand out from the crowd.

  • Haha 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...