Friday, March 02, 2007

The FizBuzz Conundrum, Ramanujan

{

Imran observed not too long ago that one proof of the difficulty of finding "quality" developers was that he (or she) was encountering a shocking amount of failure among applicants to write code that solved even simple problems. This was picked up for comment by Raganwald, Atwood, and even Hanselman.

I resisted the urge to write up a solution but after that much buzz I thought there's got to be a catch. So this morning, before I "clocked in" from my basement (we're under blizzard conditions here in South Dakota), I wrote up the first thing that came to mind:

for (int i = 1; i < 101; i++) {
Console.WriteLine((i % 3 == 0 && i % 5 == 0) ?
"Fizz-Buzz":((i %3==0)?
"Fizz":(i%5==0)?
"Buzz":i.ToString()));
}


No catches, it seems. But after thinking about it, I thought that a lot of people I work with wouldn't be happy with my solution to that particular problem. It could come across as "convoluted."

The more I thought about it, the more I knew the more acceptable answer was:

// iterate given range
for (int i = 1; i < 101; i++)
{
string output;
// if the number I'm on is divisble by 3 or 5
if (i % 3 == 0 && i % 5 == 0)
{
output = "Fizz-Buzz";
}
// if it is divisible by 3 then set output to "Fizz"
else if (i % 3 == 0)
{
output = "Fizz";
}
// if it is divisble by 5 then set output to "Buzz"
else if (i % 5 == 0) {
output = "Buzz";
}
// if divisible by neither 3 or 5, output the number.
else
{
output = i.ToString();
}
Console.WriteLine(output);
}


And as I think about it, there may be more to this test than just the matter of output - you may be able to get a sense of what type of thinking a person employs in solutions and how their values mesh with them. Even if the answer is "right" it may not be right.

A while back I was feeling some angst toward my employer (when I used to live in California) and put my resume on Monster to see what I was "worth." The first party to express interest went under the guise of an actual company, but really they were just recruiters. They scheduled me for a "test" at 8:30 am in downtown Los Angeles.

At 8:30 am in downtown Los Angeles.

Traffic and personal discomfort aside, I made it to the office where a person who was obviously non-technical gave me a (photocopied?) sheet of paper and a number two pencil to write some javascript code that manipulated the DOM and XML.

I was a little green and was writing stuff like this:

document.getElementById("foo").appendChild(root.createTextNode((entry == foo)?"This":"That")...

You get the picture. I wasn't used to writing things on paper so I was erasing here and there, and because I tended towards multiple steps in one line, my lines were going across the page and I'd curve them upward so I could get it to fit. It was pretty messy, and I'm sure the recruiter picking up the "test" wasn't impressed with how disjointed it was.

And I can imagine the people at the software company, if it ever made it there, looking at smudges and pencil marks and an utter lack of comments on a piece of paper which they'd never be able to verify unless they entered it by hand. I doubt it made it that far but if it did, they might have thought the messy code was indicative of a "messy" mind that didn't write enough comments and consolidated steps too much. It would either go into the "Not a team player" or "Quixotic" pile before it was dumped to the trash.

In defense of myself, or if I may be bold enough to think of my ideals in this light, I keep thinking about Ramanujan, the brilliant Indian mathematician. As an autodidact, he was not familiar with protocol and convention and the step by step approach other mathematicians used... he would skip steps by presenting formulas without rigorous proof in order to arrive to his solutions. Of course this is an unassailable attribute if you're as smart as a Ramanujan and a death blow if you're not.

Now Hanselman has a podcast on the whole thing. I'm going to check it out.

}

No comments: