{"id":445,"date":"2012-02-29T18:08:25","date_gmt":"2012-03-01T02:08:25","guid":{"rendered":"http:\/\/kagan.mactane.org\/blog\/?p=445"},"modified":"2012-02-29T13:42:34","modified_gmt":"2012-02-29T21:42:34","slug":"good-things-about-fizzbuzz","status":"publish","type":"post","link":"https:\/\/kagan.mactane.org\/blog\/2012\/02\/29\/good-things-about-fizzbuzz\/","title":{"rendered":"Good Things About FizzBuzz"},"content":{"rendered":"<p>Over a year ago, I mentioned <a href=\"http:\/\/imranontech.com\/2007\/01\/24\/using-fizzbuzz-to-find-developers-who-grok-coding\/\">FizzBuzz as a basic competence screen<\/a> during interviews. <a href=\"https:\/\/kagan.mactane.org\/blog\/2010\/08\/15\/why-i-dont-mind-coding-tests\/\">At the time, I said<\/a>: &#8220;My only real quarrel with FizzBuzz is that, at this point, any developer worth their salt is familiar with it.&#8221; I seem to have been wrong, becuase I keep running into coders who definitely <em>are<\/em> competent, and who haven&#8217;t encountered it&nbsp;yet.<\/p>\n<p>That&#8217;s fine, because FizzBuzz does have a few nice qualities as a&nbsp;test.<\/p>\n<h3>It&#8217;s Language-Agnostic<\/h3>\n<p>It makes sense for someone to code a FizzBuzz in anything from JavaScript to Haskell to C++ to Ruby to Python to assembler (pick your instruction set). The same cannot be said of various other standards, like &#8220;Reverse a string in-place&#8221; (which takes a bit of thought and care in C, and so makes a good test&nbsp;&mdash; but is utterly trivial in languages like Perl or PHP that have built-in string-reversal functions) or &#8220;connect to a webpage, count the frequency of words in it, and output them sorted in descending order&#8221; (which makes sense in scripting languages with well-known HTTP or cURL packages, but is far too big to make a decent whiteboard question in ones like C or&nbsp;C++).<\/p>\n<h3>It Doesn&#8217;t Require Memorization<\/h3>\n<p>There are some questions that require that the applicant knows a particular library call or special function. In the previous paragraph, I mentioned Perl&#8217;s <span class=\"code\">reverse()<\/span> and PHP&#8217;s <span class=\"code\">strrev()<\/span>&nbsp;&mdash; those aren&#8217;t what I mean, because those are pretty core functions. Those are the sort of thing anyone who does any text manipulation will have memorized, because they use it every day. But what about some of the more obscure ones, like PHP&#8217;s <span class=\"code\">strncmp()<\/span>? If you ask a question that <em>requires<\/em> it, you&#8217;re basically asking your applicant, &#8220;Have you heard of this really obscure function?&#8221; (In the case of languages like PHP, with its over-3,000 core-level functions, this is particularly evil, but the same results can be obtained by using a little-known CPAN package, C++ stdlib function, or Java Class Library&nbsp;function.)<\/p>\n<p>FizzBuzz doesn&#8217;t require any weird functions to solve. It&#8217;s nothing but math, plus a for loop and an if\/else. The most obscure thing in it is modulo arithmetic. And sure, maybe the applicant might have forgotten what the modulo operator is, because it&#8217;s not the sort of thing most people use every&nbsp;day.<\/p>\n<p>But you can still solve it without the mod operator. <!--more-->Instead of <span class=\"code nowrap\">if (foo % 3 == 0)<\/span>, you just do <span class=\"code\">if&nbsp;(int(foo&nbsp;\/ 3)&nbsp;== foo&nbsp;\/&nbsp;3)<\/span>&nbsp;&mdash; and that <em>is<\/em> the sort of thing that a decent coder should be able to figure out in a minute or two. (For <code>int()<\/code>, substitute <code>floor()<\/code> or <code>Math.floor()<\/code> or your language&#8217;s equivalent.)<\/p>\n<h3>There&#8217;s More Than One Way to Solve It<\/h3>\n<p>The infamous <a href=\"http:\/\/thedailywtf.com\/articles\/riddle-me-an-interview.aspx\">&#8220;Microsoft\/Google interview brain-teaser&#8221; puzzle questions<\/a> are notorious for the fact that there&#8217;s only one &#8220;right&#8221; answer&nbsp;&mdash; and it&#8217;s generally something that nobody could <em>possibly<\/em> figure out on their first try in an interview situation. Questions like how to figure out which light switch was on in a sealed, opaque box, or how to determine which coin weighs a different amount from a bunch of others with a minimal number of weighings&nbsp;&mdash; these basically boil down to, &#8220;Do you already know the trick behind this really contrived&nbsp;puzzle?&#8221;<\/p>\n<p>With FizzBuzz, there&#8217;s no requirement that the applicant solve it one particular way. For example, my preferred logic (expressed in pseudocode)&nbsp;is:<\/p>\n<pre class=\"code\">if (multiple of 3 and multiple of 5)\r\n&nbsp;&nbsp;&nbsp;&nbsp;print \"FizzBuzz\"\r\nelse if (multiple of 3)\r\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Fizz\"\r\nelse if (multiple of 5)\r\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Buzz\"\r\nelse\r\n&nbsp;&nbsp;&nbsp;&nbsp;print number\r\nend<\/pre>\n<p>But there&#8217;s no reason why you can&#8217;t do it this&nbsp;way:<\/p>\n<pre class=\"code\">if (multiple of 3)\r\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Fizz\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;if (multiple of 5)\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print \"Buzz\"\r\n&nbsp;&nbsp;&nbsp;&nbsp;end\r\nelse if (multiple of 5)\r\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Buzz\"\r\nelse\r\n&nbsp;&nbsp;&nbsp;&nbsp;print number\r\nend<\/pre>\n<p>Or even like&nbsp;this:<\/p>\n<pre class=\"code\">retval = \"\"\r\nif (multiple of 3) retval += \"Fizz\"\r\nif (multiple of 5) retval += \"Buzz\"\r\nif (not multiple of 3 && not multiple of 5) retval = number\r\nprint retval<\/pre>\n<p>All of these will work <em>just fine<\/em>. In fact, I&#8217;ve seen people solve it each of those ways, and given them full marks for a valid solution. And if someone asks you to solve FizzBuzz, and then gives you grief because you choose a different option than they would have? Just walk out of that interview, because that person&#8217;s just turned it back into one of those &#8220;guess what number I&#8217;m thinking of&#8221; questions that it has the potential <em>not<\/em> to be in the first&nbsp;place!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over a year ago, I mentioned FizzBuzz as a basic competence screen during interviews. At the time, I said: &#8220;My only real quarrel with FizzBuzz is that, at this point, any developer worth their salt is familiar with it.&#8221; I seem to have been wrong, becuase I keep running into coders who definitely are competent, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7,103,60],"_links":{"self":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/445"}],"collection":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":2,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/posts\/445\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kagan.mactane.org\/blog\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}