The interview “FizzBuzz” test

Interviewers have an interesting habit of using peculiar test questions to see if software developers actually know how to write code. Simply put, honesty comes before all else – if you desire to know about a candidate’s knowledge, why not ask the candidate? Sure, a candidate could lie – though not all candidates are liars. Some of us may not have the most experience on paper but have been writing code for many years. It is indeed demeaning to ask the simplest of questions during an interview – has a candidate ever asked a hiring manager to prove their capabilities as a manager? If they did, how many candidates would be hired if they proceeded to ask the most-elementary of questions?

The point is: If you are going to ask a question in an interview, it should be based on trust. Trust that if the candidate listed C# that they may have reasonable familiarity with it. Ask questions that can actually gauge the candidate’s grasp of the languages. Your company needs someone with .NET skills? Here are some nice simple questions: What can you tell me about inheritance in C# or VB.NET, and when should it be used? Or, something simple like: When’s a good time to use the StringBuilder class?

During an interview the focus should be on forming a bond between the candidate and company to determine if it is a good fit. Candidates are often stressed during interviews and this shouldn’t be held against them – remember, the primary cause of the stress may be that they really desire to be a part of your company and help achieve your goals. As anyone knows, many people that are fully qualified for a position are interviewed and never hear back from the company. This is a great stressor to some individuals.

Here is the “FizzBuzz” test in C#. Yes, there are many ways to solve it. Why did I chose THIS method? While non-multiples are the most common, they are our “default” output – therefore we must test our conditions before outputting the number. In the first example we test for a Mod 3 first because it is the most common of our criteria. If it meets the Mod 3 criteria we test for Mod 5 in case we’ve come across a “FizzBuzz” number. Yes, this means every Mod 3 is hit with a Mod 5 check – If we decided to check for a Mod 5 first we would take a bigger performance hit since there are less Mod 5’s than Mod 3’s. If we test for Mod 3 AND Mod 5 we waste cpu cycles since everything would be tested. Hence, test the Mod 3 numbers for Mod 5. ie: If it’s a Mod 3 it could be a Mod 5; but if it’s not a Mod 3 we only test to see if it is a Mod 5 from there.

Surely there is probably a slightly more efficient way to do this – just as you can swap two integer variables with the Xor trick, or swap two Strings without a temp, though of course you already knew how to do that! Hint: In .NET or VisualBasic 6 use the Left, Right, and Length functions. However, remember that while you are avoiding the creation of a new variable you are using a LOT more clock cycles to perform the “gimmick”! ..Not to mention that you will make your code significantly less human-intelligible.



            for (int i = 1; i <= 100; i++)
            {
                 if (i % 3 == 0)
                {
                    if (i % 5 == 0)
                    {
                        Debug.WriteLine("FizzBuzz");
                    }
                    else
                    {
                        Debug.WriteLine("Fizz");
                    }
                }
                else if (i % 5 == 0)
                {
                    Debug.WriteLine("Buzz");
                }
                else
                {
                    Debug.WriteLine(i.ToString());
                }
            }

Or, more fun - though the implementation choice really depends on several factors:


            string strTemp = "";

            for (int i = 1; i <= 100; i++)
            {
                if (i % 3 == 0)
                {
                    if (i % 5 == 0)
                    {
                        strTemp = "FizzBuzz";
                    }
                    else
                    {
                        strTemp = "Fizz";
                    }
                }
                else if (i % 5 == 0)
                {
                    strTemp = "Buzz";
                }
                else
                {
                    strTemp = i.ToString();
                }
                Debug.WriteLine(strTemp);
            }

Bear in mind that this code writes to the output window - if you do not have the Output windows in view you will not see any output from this code.

Just a head's up: If you came to the page in search of the answer to this question, well, you need to get in front of an IDE more often and write the code. Why an IDE? Many IDE's can tell you what you are doing wrong as you type your code. I started writing code on Visual Basic 3.0 and even THAT could tell you that what you just typed was full of errors! It was a great learning tool since rather than put little squiggly-lines under the code it would throw a MessageBox in your face and yell at you - complete with the Windows95 "Alert" .wav sound. A nice little punishment for your errors that taught you quickly.

David R. Betournay

Leave a Reply