Further Into FizzBuzz!

by Joey deVilla on November 16, 2012

As I write this, StatCounter is reporting that my recent post on giving the FizzBuzz test to developer job candidates has received over 21,000 pageviews. Without any prompting from me, my friend Reg “Raganwald” Braithwaite (we both live in Toronto and see each other from time to time) linked to it from both r/programming and Hacker News, and the discussion that first started five years ago with Imran Ghory’s blog post began anew. I thought I’d respond to questions posted in both the comments and forums here, as well as explain a few things.

A Quick Introduction

We’re a startup. My cofounder, Jesse Singh, is bootstrapping the company with his own money. We’re creating two different applications: one that does cross-platform mobile device management, and another that performs a series diagnostic tests on mobile devices. We’re currently paying the bills and getting in some market research by doing mobile strategy assessments for enterprises and organizations. We’ve also been approached about developing mobile “dashboard” applications that provide a high-level view of supply chains. We’re also rewriting our crappy, crappy website. And finally, we’re making sure that money’s coming in, whether it’s from customers or funding.

The positions we were looking to fill were:

  • A web-and-back-end developer (PHP/MySQL)
  • An iOS developer

A company like ours, at this particular stage of its evolution, often requires its people to not only perform their jobs really, really well, but to also take on a number of other roles as the situation dictates. Circumstances have already required us to — I hate to use the word, but it’s appropriate — pivot. We’re dealing with limited resources, long enterprise sales cycles, the moving-target nature of mobile development and Murphy’s Law. When it comes to hiring, we can’t afford not to be picky.

When Candidates Got FizzBuzz Wrong

That’s me: High Expectations Asian CTO.

In the comments, Vasi wrote:

I’m curious what “failures” look like. Is it complete inability to get anything done at all? Or relatively small mistakes, like ending at 99 instead of 100?

One candidate took longer than 10 minutes while working on FizzBuzz. Since we had only an hour for each interview and a number of candidates to go through, we I had to set that time limit. The candidate had decades of experience, was the one who asked “Do you think I should use a loop?”, and handed this in when time was up:

// Written in C
for (i = 1; i <= 100; i++) {

// A whole lotta stuff scratched out

}

I’m sorry; this is just plain unacceptable.

The most common mistake followed this pattern:

// Written in PHP
if ($i % 3 == 0) echo "Fizz<br />";
if ($i % 5 == 0) echo "Buzz<br />";
if ($i % 15 == 0) echo "FizzBuzz<br />";
echo ($i . "<br />");

I consider knowing when to use a series of if statements and when to use if-else if-else to be pretty fundamental, especially for a problem as simple as FizzBuzz.

I was willing to let the classic “using the assignment operator when you meant to use the equality operator” mistake slide:

if (i % 3 = 0)

I was less willing to let this mistake go unremarked:

// Written in PHP
if ($i % 3) {
  echo "Fizz<br />";
} elseif ($i % 5) {
  echo "Buzz<br />";
} elseif ($i % 15) {
  echo "FizzBuzz<br />";
} else {
  echo ($i . "<br />");
}

Even if the mistakes in the conditionals were fixed, there would still be a problem with the code. Please tell me you can spot it.

Modulo Math

Max Howell was the first of a number of commenters to write:

Is the modulus operator domain knowledge? I smirk at the concept. But if an interview asked me to do some bit operations with ~|& etc. it would take me longer than five minutes, and if I had to use ^ (XOR) I’d probably be screwed.

Good question. Our company makes mobile apps for business, and the candidates were made aware of that. I consider the modulus (%) operator to be one of those things that you need to know about in programming for business, because there are many cyclical things that you deal with: days of the week, displaying tables with lines in alternating colours and so on.

On the other hand, using XOR — I’m presuming that Max is referring to the handy-dandy trick for swapping the values in two variables without the use of a temporary variable — isn’t as applicable to the sort of apps we’re writing. However, if we were in the business of writing games or other sorts of apps where bit-twiddling is important, then knowing how to use XOR and its ilk would be important.

The Smartass Solution

Timothy asked what would’ve happened if someone had submitted this solution:

// Written in C#
Console.WriteLine(1);
Console.WriteLine(2);
Console.WriteLine(“Fizz”);
Console.WriteLine(4);
Console.WriteLine(“Buzz”);
…
Console.WriteLine(14)
Console.WriteLine(“FizzBuzz”)
Console.WriteLine(16)
…

He pointed out that not only does this solution works, it uses less CPU power than a solution involving a loop and branching.

I replied that if it were apparent to me that a candidate submitted such a solution and made it clear to me that it was submitted in the spirit of smartassery, I’d give the canddiate points for chutzpah. I’d also give the candidate points for arguing that it’s less CPU-intensive; after all, we are a mobile app company, and battery life is one of those things that our developers have to consider.

Of course, I’d then have to follow up with “Could you give me another implementation, please? Perhaps one using looping and branching?”

Why Bother Defining multiple_of_3 and multiple_of_5?

For these reasons:

  1. It’s an anti-stupid-mistake measure. I’m less likely to miswrite multiple_of_3 than (i % 3 == 0).
  2. It’s DRY.
  3. To demonstrate more than just technical competence, but also a cultural understanding of Ruby. Ruby convention favours lots of short methods. See Eloquent Ruby for details and Shopify’s ActiveMerchant library for examples.
  4. It clearly communicates my intent to the person reading the code, who just happens to be someone evaluating me for a job position.
If I’d been even more clever, I’d have named the methods fizz? and buzz? instead.

“Is This Some Kind of Trick Question?”

Mouad suggested that candidates might believe that the test is too simple and suspect that there’s some kind of trick. I assured them that it was not a trick question and should be taken at face value.

The Sample Size

E.Z. Hart asked:

Out of curiosity, how many is ‘some programmers’? I’m just wondering what the sample size is here.

We’ve promised to certain parties to keep this number vague. The best I can do is say that the total number of candidates could fit in a standard city bus.

In Which Your Humble Narrator Gets Called a D-Bag

Peter wrote:

Giving programming tests in interviews is bullying in the most pathetic passive-aggressive way possible. The programming field is full of dbags and this type of interview test is an example of what happens when said dbags are given the chore of interviewing.

D-bag? A comment as stand-out as this calls for an equally stand-out reply:

But seriously: it would’ve been a d-bag move had the purpose of the test been to reinforce my ego by trampling on candidates, to trip them up by requiring some esoteric knowledge or for some other equally malicious purpose. That’s why I used FizzBuzz; it tests basic aptitude and it’s over fairly quickly. This job is going to be stressful, and if FizzBuzz is giving you palpitations, you’re not going to like the real thing.

A far more difficult test was the one given to me when I flew down to Palo Alto this summer and interviewed at Pebble to be their developer evangelist (I didn’t get the job, but then again, no one else did). They interviewed me over two days, and on day one, they tested me by telling me to build an app — any app — for their previous smartwatch, the inPulse, just to prove that I could code. There was no spec, not much in the way of documentation, but looking at some example code and the comments in the API header files, I was able to put together a working “Magic 8-Ball” app in less than an hour. (I later turned that app into this tutorial.) I considered the test tough, but fair, because their project is a high-stakes one.

Our company doesn’t face the same level of risk as Pebble, but it too is a startup, and we’re taking on risk ourselves. Jesse’s bootstrapping the company, which means that until we get customers or funding, everyone’s salary is coming out of his pocket. I feel completely justified in taking fair measures that ensure that we’re not wasting his money.

What Else Did We Ask Candidates?

Yes, we asked them to show us projects that they worked on and saw samples of their code. Not all candidates were able to provide samples; some didn’t have code from their previous work, some of their projects were internal web apps that couldn’t be set up on a personal laptop, and some didn’t have side projects they could show me.

I also had each candidate do a quick code review with me of code written by an earlier developer for our applications. I explained what that code was supposed to do and showed them the running application in action. We looked over the code and I asked the candidates to tell me what they thought of it. While the code worked, it was a big ball of mud with plenty of room for improvement. A number of the candidates said “That looks like something I would write! It’s good!”, while others came up with a number of good suggestions for improving it.

What Purpose Did the Recruiting Company Serve?

They helped bring in a good number of candidates, and they also helped pick out people who would be a good fit for the company. They spent a fair bit of time talking to me and Jesse to get a feel for the company culture and how we worked. We explained to them that we needed people who were self-starters and comfortable with the sort of risks and uncertainty that would come with the territory. We told them not to worry too much about the technical aspects of the candidate, as Jesse and I would be handling that.

The candidates who came in via the recruiter were interviewed by them first, after which those deemed to be a good match for us were then sent our way.

What About Candidates Who Were Having a “Bad Brain Day”?

I kept that in mind. That’s why we didn’t rely solely on FizzBuzz. Remember: I’ve been doing either technical evangelism or developer relations for the past ten years. I think I know a think or two about “reading” developers; in fact, at Microsoft, I took some pretty extensive training on that topic.

{ 9 comments… read them below or add one }

1 Jordan Morgan November 16, 2012 at 11:22 am

“I kept that in mind. That’s why we didn’t rely solely on FizzBuzz. ” Regarding the last paragraph “What about candidates who were having a bad brain day?”, I thought you mentioned earlier in the first post that if they didn’t pass FizzBuzz, they were not considered?.

Anyways, great post. I am about to graduate and have been looking around for questions that I might encounter when I go to interview for my first “big boy” job.

2 Joey deVilla November 16, 2012 at 11:36 am

Jordan Morgan: I still considered people who failed FizzBuzz and followed through with the rest of the interview, accounting for the possibility that they were having an off-day. It turned out that they weren’t right for us for one reason or another — too “copy and paste, adjust to taste”, not suited for startup work and so on. In the end, not a single person who failed FizzBuzz made the cut, but it wasn’t just FizzBuzz alone that determined that.

Good luck with your interviews!

3 Randy November 16, 2012 at 12:39 pm

The problems with this:

// Written in PHP
if ($i % 3) {
echo “Fizz”;
} elseif ($i % 5) {
echo “Buzz”;
} elseif ($i % 15) {
echo “FizzBuzz”;
} else {
echo ($i . “”);
}

are, of course, that we want the modulus to be zero (that’s the issue with conditionals you identified, right?), and that the % 15 case will never be reached – the %3 case will always go first.

Also, % 15 is a pretty opaque way of saying “divisible by 3 and 5″.

4 Dac Chartrand November 16, 2012 at 12:42 pm

Hi Joey, you may remember me from such Microsoft contest winners as the guy who wrote a CMS based around the idea of Naive Bayesian categorization and probabilistic content. Code on SourceForge available for review, keyword: sux0r.

I read the last two blog posts you wrote about this and I don’t think I would have passed this test.

The issue for me is the way you worded the problem with the idea that this was a job interview. Had you said “Hint: use the modulo operator” I would have been, oh fuck, yeah easy.

IMHO FizzBuzz is a bad benchmark because it’s not a real problem. It basically combines math and complete “ivory tower” uselessness. Someone like me will get stumped because I will keep asking myself: “How is this related to any problem I will ever need to solve? I’m going to get paid to do this?” To me it’s the difference between going to the gym doing squats and knowing how to fight.

To quote you: “I was able to put together a working “Magic 8-Ball” app in less than an hour [...] I considered the test tough, but fair.” -That’s more my game. Or a hackathon.

Anyways, congratulations. I enjoyed both articles. Always nice to remember that I forget. It’s like those detective books where at the end I’m all like, I knew it! (but not before, haha) I hope you found the right candidate. 

5 chip November 16, 2012 at 2:09 pm

Were any candidates sufficiently dedicated to TDD that they wrote tests (on paper) before writing the code?

6 Wesley Kerfoot November 16, 2012 at 2:47 pm

It seems like the ruby convention of using ? to represent functions that return a boolean was taken from lispers, who tend to use that a lot. In fact it seems like Ruby has taken a lot from lisp in general >.>

Anyway, it took me a second to find the problem with the PHP code, probably because I was trying to figure out why they would forget to do the “== 0″ part if they knew that “n % 15“ is true for numbers that are both divisible by 5 and 3. Maybe they thought 0 gets converted to True in PHP? IIRC it would be false. I won’t say what the problem is so other people can try and see.

7 Corey Punches November 16, 2012 at 2:54 pm

I always enjoy reading this type of article, and I thank my lucky stars that I rarely need to hire developers these days. Having moved on from being a full-time programmer I decided to give your test a quick try and was happy that I was able to hack out a solution in just a couple of minutes. Just in case you ever hire a PL/SQL dev, here’s a possible solution:
SQL> set serveroutput on
SQL> begin
2
3 for v_cnt in 1..100 loop
4 if ( mod(v_cnt,3) = 0 AND mod(v_cnt,5) = 0) then
5 dbms_output.put_line(‘FizzBuzz’);
6 elsif ( mod(v_cnt,3) = 0 ) then
7 dbms_output.put_line(‘Fizz’);
8 elsif ( mod(v_cnt,5) = 0 ) then
9 dbms_output.put_line(‘Buzz’);
10 else
11 dbms_output.put_line(v_cnt);
12 end if;
13 end loop;
14
15 end;
16 /
:)

8 Mike November 16, 2012 at 7:31 pm

I took the FizzBuzz challenge (at first I thought you were challenging us to a game of FizzBin, but…). Problem was, I couldn’t for the life of me remember the operator in C# for modulo! I ended up with:

for (int x = 1; x < 101; x++)
{
Console.WriteLine("{0}", ((float)(x / 3) == (float)x / 3.0f) && (float)(x / 5) == (float)x / 5.0f ? "FizzBuzz" : (float)(x / 3) == (float)x / 3.0f ? "Fizz" : (float)(x / 5) == (float)x / 5.0f ? "Buzz" : x.ToString());
}

So, would you have strung me up by my thumbs or just laughed me out of the office?

9 david November 17, 2012 at 1:06 am

Not trying to be a dbag, because you can obviously code. But I still think that in this particular case, splitting out the multiple_of_3? and multiple_of_5? functions was overkill. In other cases, it might be a good idea, but in this case it makes the code needlessly verbose.

My code with and without the use of modulo

def fizzbuzz
for i in 1..100
if i % 3 == 0 && i % 5 == 0
puts “FizzBuzz”
elsif i % 3 == 0
puts “Fizz”
elsif i % 5 == 0
puts “Buzz”
else
puts i
end
end
end

def fizzbuzz2
three_count = 0
five_count = 0
for i in 1..100
three_count += 1
five_count += 1
if three_count == 3 && five_count == 5
puts “FizzBuzz”
three_count, five_count = 0, 0
elsif three_count == 3
puts “Fizz”
three_count = 0
elsif five_count == 5
puts “Buzz”
five_count = 0
else
puts i
end
end
end

Leave a Comment

{ 1 trackback }

Previous post:

Next post: