[QUIZ] FizzBuzz (#126)

R

Ruby Quiz

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

There has been some debate on the proper ways to screen programmers you intend
to hire. A common theory is that you really need to have the programmer write
some code for you to accurately gauge their skill. Exactly what to have them
write is another debate, but the blogosphere has recently been abuzz with this
question as a screener:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz†instead of the
number and for the multiples of five print “Buzzâ€. For
numbers which are multiples of both three and five
print “FizzBuzzâ€.

Pretend you've just walked into a job interview and been hit with this question.
Solve it as you would under such circumstances for this week's Ruby Quiz.
 
H

Hans Fugal

Ruby said:
There has been some debate on the proper ways to screen programmers you intend
to hire. A common theory is that you really need to have the programmer write
some code for you to accurately gauge their skill. Exactly what to have them
write is another debate, but the blogosphere has recently been abuzz with this
question as a screener:

Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz†instead of the
number and for the multiples of five print “Buzzâ€. For
numbers which are multiples of both three and five
print “FizzBuzzâ€.

Pretend you've just walked into a job interview and been hit with this question.
Solve it as you would under such circumstances for this week's Ruby Quiz.

Done, 4 times over. Not a bad quiz though my answers will probably be
considered boring and would be denounced by the fools using this tactic
to hire people as "unimaginative" or "lacking creativity and whizbang
problem-solving skills".

I feel compelled to point out that it was Microsoft who started this
whole mess, and they quickly figured out it was selecting the wrong
people and they don't do it anymore. But the cat's out of the bag, and
now every wannabe shop thinks that's the way to find smart people. It's
not, it's the way to find people who are good at solving puzzles under
pressure, and in the worst case to find people who are good at finding
ridiculously clever (but unreadable/unmaintainable) answers to puzzles
under pressure.

As we all know, good software development is not about solving little
puzzles. It's about reasoning about problems large and small and finding
the most obvious and readable solution (and sometimes optimizing the
inner loop).

Maybe I would try this in a job interview. I might state the problem and
say: "this is not a trick question nor hard - just write the code they
way you normally would." If they write the boring answer, they get
points in my book. If they write some clever answer, they get demerits.
If they can't write code at all, and I didn't realize they wouldn't be
able to, I go sit in a corner and pout.
 
P

Paul Novak

Done, 4 times over. Not a bad quiz though my answers will probably be
considered boring and would be denounced by the fools using this tactic
to hire people as "unimaginative" or "lacking creativity and whizbang
problem-solving skills".

I feel compelled to point out that it was Microsoft who started this
whole mess, and they quickly figured out it was selecting the wrong
people and they don't do it anymore. But the cat's out of the bag, and
now every wannabe shop thinks that's the way to find smart people. It's
not, it's the way to find people who are good at solving puzzles under
pressure, and in the worst case to find people who are good at finding
ridiculously clever (but unreadable/unmaintainable) answers to puzzles
under pressure.

As we all know, good software development is not about solving little
puzzles. It's about reasoning about problems large and small and finding
the most obvious and readable solution (and sometimes optimizing the
inner loop).

Maybe I would try this in a job interview. I might state the problem and
say: "this is not a trick question nor hard - just write the code they
way you normally would." If they write the boring answer, they get
points in my book. If they write some clever answer, they get demerits.
If they can't write code at all, and I didn't realize they wouldn't be
able to, I go sit in a corner and pout.

I do not think there is a lot of room for cleverness in the FizzBuzz
problem. (Actually, I hope I am wrong and we see lots more
suggestions for Extra Fun like Peter's.) My understanding is that
FizzBuzz was intended to screen out candidates that could *not* come
up with working code in a few minutes.

Regards,

Paul.
 
R

Robert Dober

Me too I consider this a very good psychological quiz.
My solution is ready and it was a compromise between being clever,
liking the code I have written and *maybe* getting the job.
Given that it took me almost 10 minutes to find that compromise and I
am really looking forward to getting some feedback about the
impression such a solution would make.
James do you have some surprise for us like interpretation from recruiters?

This is an odd looking quiz but I guess it might become one of the
most interesting ones, actually it already is, given this discussion
;)

Cheers
Robert
 
J

James Edward Gray II

Perhaps it's a test of personality more than one of performance
under pressure.

I happen to agree and I worded the quiz very carefully with that in
mind.

James Edward Gray II
 
J

James Edward Gray II

James do you have some surprise for us like interpretation from
recruiters?

I think we're already getting plenty of insightful interpretation.

James Edward Gray II
 
J

James Edward Gray II

Done, 4 times over.

I almost didn't run it, for this reason. Solutions are everywhere, =20
even in Ruby.

I do think it's an interesting problem though, more because of what =20
it's looking for (whatever you believe that is) than the code. I =20
think how you solve it is important.

James Edward Gray II
 
H

Hans Fugal

James said:
I almost didn't run it, for this reason. Solutions are everywhere, even
in Ruby.

Oh, I meant I had done it in four ways since the quiz had been posted.

Re the comment about not being one of the ping pong balls in a jumbo jet
questions, that's true.

Having not heard of this particular problem, I jumped to the conclusion
that it was one of those "write this seemingly simple program but you're
not cool unless you do it the clever way I'm thinking of" problems. If
it's really used as just a "can you write a simple program" question,
I'm all for that.
 
G

Giles Bowkett

I do not think there is a lot of room for cleverness in the FizzBuzz
problem. (Actually, I hope I am wrong and we see lots more
suggestions for Extra Fun like Peter's.) My understanding is that
FizzBuzz was intended to screen out candidates that could *not* come
up with working code in a few minutes.

I have the exact same understanding about the intention of FizzBuzz,
but I disagree about the lack of room for cleverness. I've seen it
solved with lazy evaluation and with a Lisp interpreter that ran a
Prolog interpreter (both solutions were written in Ruby).

I've even seen it **unit tested**. (That solution was in Java.)

I wrote some pretty cranky anti-FizzBuzz blog posts a while ago but
still succumbed to the peer pressure and wrote my own (which contains
a small amount of unnecessary code which I didn't spot at the time).

Won't post til the quiz deadline, of course, but just wanted to say, I
think there's a lot of room for cleverness here, although I sometimes
think the clever solutions are a bit perverted in the brainpan.
 
D

Daniel Martin

Hans Fugal said:
I feel compelled to point out that it was Microsoft who started this
whole mess, and they quickly figured out it was selecting the wrong
people and they don't do it anymore. But the cat's out of the bag, and
now every wannabe shop thinks that's the way to find smart
people. It's not, it's the way to find people who are good at solving
puzzles under pressure, and in the worst case to find people who are
good at finding ridiculously clever (but unreadable/unmaintainable)
answers to puzzles under pressure.

I think the usefulness of this interview technique depends on what
problem is used and what it is used for. Namely, if the problem is
one that can be solved quickly, and while on autopilot, by pretty much
any decent programmer, and if it is being used simply as a straight
yes/no screen to block people who can't program their way out of a wet
paper bag, then I see "write code for this" as a useful interviewing
tool. Maybe it isn't a problem where you are, but we've had people
claiming five years of programming experience who were unclear on how
two nested loops would execute.

For example, one question we frequently use is "write a function that
takes a two dimensional array and returns the sum of the minimum value
in each row". Now, we understand that people are nervous in
interviews, and we'll occasionally give hints such as "this line here
throws a NullPointerException when you run it", or "that works, unless
the array contains a negative number", and give the candidate a chance
to revise their answer. However, we have honestly had people
interview who couldn't write a doubly nested loop, or even explain
coherently how a sample solution worked. Maybe it'd be easier if we
were interviewing for ruby programmers instead of java programmers.

I'll also point out that there's a quite prestigious shop located in
Mountain View, CA that handed me a whole bunch of "write code in
language X that does this" problems when I interviewed there. (and
they *still* didn't take me - http://xkcd.com/c192.html )

On penalizing ridiculous cleverness:

When my (now current) boss gave me that question above, she said
initially "in any language". I scribbled something really short down,
and asked "you probably meant any imperative language, right?", after
which I gave a completely unimaginative imperative solution in
perl. (The language I was most familiar with at the time)

I don't go for the overly clever bizarre stuff at work - that's what
Ruby Quiz is for. Well, okay, I have been known to write bizarre perl
one-liners at the shell prompt, but not for anything that's going to
be run more than once. I have however been known to occasionally use
techniques that some people might call obscure in production code if
the code overall becomes more elegant or easier to read. I'd much
rather build a parser that can read the description of a data record
as provided by a vendor than have fifty lines of mostly cut-and-paste
but conceptually "simple" code that doesn't tell me at a glance
whether we're parsing the latest version of the format.

The point is, yes, maintainability is important, but I really don't
understand the animosity towards "cleverness" that seems to be showing
up here. That sounds to me dangerously close to the same attitude
that keeps ruby and many other non-mainstream languages out of many
production environments on the grounds that "we'll never be able to
find anyone to maintain it".

Also, as a practical matter, I haven't seen this "cleverness" in any
of the subsequent interviews I've conducted from the other side of the
desk. Frankly, I'd love some evidence that incoming candidates had
been exposed to something other than the industry standard languages
and platforms. Exposure to functional programming and the thought
patterns that go with it is a *good* thing. Being able to look at a
problem from multiple angles is a *good* thing. Mental agility?
We're supposed to like that in a candidate.

Incidentally, my initial answer was:

(Plus @@(Min /@ #))&

Anyone care to identify the language?
 
M

Mike Mondragon

I think the usefulness of this interview technique depends on what
problem is used and what it is used for. Namely, if the problem is
one that can be solved quickly, and while on autopilot, by pretty much
any decent programmer, and if it is being used simply as a straight
yes/no screen to block people who can't program their way out of a wet
paper bag, then I see "write code for this" as a useful interviewing
tool. Maybe it isn't a problem where you are, but we've had people
claiming five years of programming experience who were unclear on how
two nested loops would execute.

For example, one question we frequently use is "write a function that
takes a two dimensional array and returns the sum of the minimum value
in each row". Now, we understand that people are nervous in
interviews, and we'll occasionally give hints such as "this line here
throws a NullPointerException when you run it", or "that works, unless
the array contains a negative number", and give the candidate a chance
to revise their answer. However, we have honestly had people
interview who couldn't write a doubly nested loop, or even explain
coherently how a sample solution worked. Maybe it'd be easier if we
were interviewing for ruby programmers instead of java programmers.

I'll also point out that there's a quite prestigious shop located in
Mountain View, CA that handed me a whole bunch of "write code in
language X that does this" problems when I interviewed there. (and
they *still* didn't take me - http://xkcd.com/c192.html )

On penalizing ridiculous cleverness:

When my (now current) boss gave me that question above, she said
initially "in any language". I scribbled something really short down,
and asked "you probably meant any imperative language, right?", after
which I gave a completely unimaginative imperative solution in
perl. (The language I was most familiar with at the time)

I don't go for the overly clever bizarre stuff at work - that's what
Ruby Quiz is for. Well, okay, I have been known to write bizarre perl
one-liners at the shell prompt, but not for anything that's going to
be run more than once. I have however been known to occasionally use
techniques that some people might call obscure in production code if
the code overall becomes more elegant or easier to read. I'd much
rather build a parser that can read the description of a data record
as provided by a vendor than have fifty lines of mostly cut-and-paste
but conceptually "simple" code that doesn't tell me at a glance
whether we're parsing the latest version of the format.

The point is, yes, maintainability is important, but I really don't
understand the animosity towards "cleverness" that seems to be showing
up here. That sounds to me dangerously close to the same attitude
that keeps ruby and many other non-mainstream languages out of many
production environments on the grounds that "we'll never be able to
find anyone to maintain it".

Also, as a practical matter, I haven't seen this "cleverness" in any
of the subsequent interviews I've conducted from the other side of the
desk. Frankly, I'd love some evidence that incoming candidates had
been exposed to something other than the industry standard languages
and platforms. Exposure to functional programming and the thought
patterns that go with it is a *good* thing. Being able to look at a
problem from multiple angles is a *good* thing. Mental agility?
We're supposed to like that in a candidate.

Incidentally, my initial answer was:

(Plus @@(Min /@ #))&

Anyone care to identify the language?


I worked with a lead in a java shop that liked to ask this question:

"Write a function that reverses the *words* in a string" (in java)

Firstly, it would reveal if the candidate listens to the problem and
then goes about logically solving what was communicated to them (i.e.
its not reversing the string character by character). If they get
crafty with the problem then kudos to them, but if they get stuck
she'll say that its ok to use java util classes to help solve the
problem. Secondly if stuck since they claim to be java programmers
then they should know about the StringTokenizer combined with Stack
classes that makes this problem trivial to solve.

At first I thought this question was too simplistic (much like
FizzBuzz), but it really does fletch out the Heros from the Zeros.
 
C

Chris Shea

I worked with a lead in a java shop that liked to ask this question:

"Write a function that reverses the *words* in a string" (in java)

Firstly, it would reveal if the candidate listens to the problem and
then goes about logically solving what was communicated to them (i.e.
its not reversing the string character by character). If they get
crafty with the problem then kudos to them, but if they get stuck
she'll say that its ok to use java util classes to help solve the
problem. Secondly if stuck since they claim to be java programmers
then they should know about the StringTokenizer combined with Stack
classes that makes this problem trivial to solve.

At first I thought this question was too simplistic (much like
FizzBuzz), but it really does fletch out the Heros from the Zeros.

Isn't your test ambiguous? Do you want "Hello World" to become "World
Hello" or "olleH dlroW"? Or is that part of the test?
 
M

Mike Mondragon

Isn't your test ambiguous? Do you want "Hello World" to become "World
Hello" or "olleH dlroW"? Or is that part of the test?

Ah yes, thanks for jogging my memory, "Write a function that reverses
the words of a string"
 
R

Rob Biedenharn

Ah yes, thanks for jogging my memory, "Write a function that reverses
the words of a string"

I'd think that would be another opportunity to see how the person
works. Do they notice the potential ambiguity of the requirement?
If not, how do they react when the other interpretation (from the one
they assumed) is pointed out?

Of course, giving one of the specifications:

assert_equal "World Hello", reverse_words("Hello World")
assert_equal "olleH dlroW", reverse_words("Hello World")

would clear the matter up (when asked). (And it's been a couple
years since I've looked at JUnit so just imagine the equivalent Java
syntax ;-)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
D

Daniel Martin

Hans Fugal said:
Having not heard of this particular problem, I jumped to the
conclusion that it was one of those "write this seemingly simple
program but you're not cool unless you do it the clever way I'm
thinking of" problems. If it's really used as just a "can you write a
simple program" question, I'm all for that.

Right, which is why FizzBuzz or the minsum question I mentioned (or
even the "World Hello" problem) are fair game in an interview, but
"how do you detect a loop in a singly linked list in an efficient,
O(n) manner" is just not. (Unless the candidate has just made the
claim that they've read all three of Knuth's books thoroughly and are
being an ass about it)

(Incidentally, I know of two different solutions to that problem,
neither of which are likely to ever be of any use to me at all.)
 
R

Rick DeNatale

Like many others, I knocked this out in a few minutes, while I was eating lunch.

I then decided, like others, to try to do it different ways. I came
up with 6 different solutions in about an hour, ranging from quite
straightforward to bizarre and cryptic.

I hope that those who are familiar with my musings, ponderings and
pontifications will realize that the latter ones were out of my
wheelhouse.

ISTR that this problem came up not too long ago in the code golfing
community. I won't go that far!
 
A

Ari Brown

Hey all, I have a quick question on this quiz:
I have a working version of it, but I just want to compare and see
what everyone else got.

How many lines do you have it written in?

That's my big problem - making programs too long.

thanks,
~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top