RH | 5,422 Posted April 14, 2020 Share Posted April 14, 2020 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. 1 Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/ Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 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. 1 Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68371 Share on other sites More sharing options...
RH | 5,422 Posted April 14, 2020 Author Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68372 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 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. 3 Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68373 Share on other sites More sharing options...
MachineCode | 159 Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68381 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 Here's a quickie using Vue cuz I'm hip and cool and totally on top of the latest trends: https://codepen.io/DouglasGlover/pen/VwvvXXv 1 Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68384 Share on other sites More sharing options...
RH | 5,422 Posted April 14, 2020 Author Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68386 Share on other sites More sharing options...
Lincoln | 230 Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68399 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 Just now, Lincoln said: And then who knows how it gets filtered through management types before winding up in an interview. Google search "programmer interview questions" Copy-pasta Hire the most skilled candidate! Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68401 Share on other sites More sharing options...
MachineCode | 159 Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by MachineCode Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68402 Share on other sites More sharing options...
RH | 5,422 Posted April 14, 2020 Author Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68409 Share on other sites More sharing options...
MachineCode | 159 Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by MachineCode Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68416 Share on other sites More sharing options...
Daniel_Doyce | 497 Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68434 Share on other sites More sharing options...
MiamiSlice | 3,226 Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by MiamiSlice Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68439 Share on other sites More sharing options...
MiamiSlice | 3,226 Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68441 Share on other sites More sharing options...
Daniel_Doyce | 497 Posted April 14, 2020 Share Posted April 14, 2020 I'm assuming you will get dinged for outputting "Fizz " instead of "Fizz" or "FizzBuzz" instead of "Fizz Buzz" since they are two different strings. If not, then the solution is much easier. Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68443 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 That's easy to fix using javascripts built in trim() function, which removes trailing and leading spaces. Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68444 Share on other sites More sharing options...
Daniel_Doyce | 497 Posted April 14, 2020 Share Posted April 14, 2020 But you need the space in the second case (div by 15) and shouldn't have it in the first (div by 3 and not 5), which requires a check for mod 3 and mod 5 in some manner to distinguish between the two. Also, string operators like trim() are relatively computationally intensive. Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68447 Share on other sites More sharing options...
Daniel_Doyce | 497 Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by Daniel_Doyce Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68451 Share on other sites More sharing options...
Lincoln | 230 Posted April 14, 2020 Share Posted April 14, 2020 (edited) 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 April 14, 2020 by Lincoln Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68453 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68454 Share on other sites More sharing options...
Daniel_Doyce | 497 Posted April 14, 2020 Share Posted April 14, 2020 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 https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68455 Share on other sites More sharing options...
Lincoln | 230 Posted April 14, 2020 Share Posted April 14, 2020 Ah yeah i see it now. Still, its too clever for the exercise. Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68456 Share on other sites More sharing options...
Gloves | 12,583 Administrator · Posted April 14, 2020 Share Posted April 14, 2020 I added a piece to my Vue example which lets you input any number and it'll print out that many lines of FizzBuzz instantly below. ... Added another 9 and it took a moment, but it printed it out nonetheless: Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68461 Share on other sites More sharing options...
Reed Rothchild | 10,067 Editorials Team · Posted April 14, 2020 Share Posted April 14, 2020 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. 1 Link to comment https://www.videogamesage.com/forums/topic/3285-software-devs-anyone-ever-been-asked-the-fizzbuzz-question-on-an-interview-how-did-you-solve-it/#findComment-68476 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now