Can you beat 99% of programmers? Try the FizzBuzz challenge
trybloc.com
trybloc.com
The FB-capable average 1.3 interviews/year. The FB-failures average 150 interviews/year. (All numbers are total fabrications, of course.) There don't have to be a lot of FB-failures for them to represent 99% of interview candidates.
I am still dismayed at how few programmers can solve this given 30 min (15 for the correct solution, plus 15 for optimizations across instructions and code brevity). It filters out the weak for sure.
For example a good developer should be able to solve this problem even if he has never used the technologies involved. It is that simple of a problem. I have never developed in Ruby in my life, 10 minutes on Google gave me all the Ruby API's I needed to complete the challenge. You see a good developer sees the solution and then figures out the API to do what they need it to do. Mastery of the technology is not what makes a good developer, seeing the solutions is where the value of a developer should be measured.
http://www.codinghorror.com/blog/2007/02/why-cant-programmer...
My own opinion is that if the ratio is this bad by the time they get to the interview, you may not be pre-screening well enough.
puts ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz", "16", "17", "fizz", "19", "buzz", "fizz", "22", "23", "fizz", "buzz", "26", "fizz", "28", "29", "fizzbuzz", "31", "32", "fizz", "34", "buzz", "fizz", "37", "38", "fizz", "buzz", "41", "fizz", "43", "44", "fizzbuzz", "46", "47", "fizz", "49", "buzz", "fizz", "52", "53", "fizz", "buzz", "56", "fizz", "58", "59", "fizzbuzz", "61", "62", "fizz", "64", "buzz", "fizz", "67", "68", "fizz", "buzz", "71", "fizz", "73", "74", "fizzbuzz", "76", "77", "fizz", "79", "buzz", "fizz", "82", "83", "fizz", "buzz", "86", "fizz", "88", "89", "fizzbuzz", "91", "92", "fizz", "94", "buzz", "fizz", "97", "98", "fizz", "buzz"]
I passed.
puts %w(1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz fizz 22 23 fizz buzz 26 fizz 28 29 fizzbuzz 31 32 fizz 34 buzz fizz 37 38 fizz buzz 41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz fizz 52 53 fizz buzz 56 fizz 58 59 fizzbuzz 61 62 fizz 64 buzz fizz 67 68 fizz buzz 71 fizz 73 74 fizzbuzz 76 77 fizz 79 buzz fizz 82 83 fizz buzz 86 fizz 88 89 fizzbuzz 91 92 fizz 94 buzz fizz 97 98 fizz buzz)
1. Google to see if Ruby has a for loop. It does. "for i in 1..100" looks like a good start.
2. Google for a switch or case statement. Don't like what I see. Google for an if/else. Looks good. Guess "if i%15 == 0 ... elsif ... end".
3. Google to make sure the mod operator is indeed %.
Done.
Cleaner, I think is "1.upto(100){ ... }"
If anyone wants to laugh at my code, here it is: https://github.com/tzs/WAR-Whom
I also did the same thing with JavaScript. I wanted a calculator for Talisman building in Warhammer Online. At first I was going to do it as an iPhone application. Then I decided a web page would make more sense, and so was going to write it as a Perl CGI application. Then I decided it would make more sense to do it in JavaScript so that my server wasn't doing the work.
That code is here: https://github.com/tzs/WAR-TalismanCalculator
These were fun experiments, but overall I think I prefer to learn the way I learned C. I got K&R, read it, then went to the computer center (this was before personal computers were common), sat down at a terminal, and started hacking out some simple programs, with my K&R at my side. A couple sessions of that cleared up the few things I had misunderstood.
Unfortunately, I've never found a book as good as K&R for any other language I've wanted to learn.
I remember reading an article about "Digital Natives" and "Digital Immigrants". The thesis of the article was that people who grow up in the digital world have a fundamentally different approach to things like learning than do those of us who grew up pre-digital. They are the natives--they grew up with computers, and multitasking, and access to vast amounts of online knowledge. They don't want to learn things by sitting down with a book and reading it through chapter by chapter, and then go out and apply that knowledge. They expect that for any task they wish to accomplish, they can learn what they need when they need it by going to Wikipedia, or Googling, or getting help on IRC, and so on.
When I set out to write my Warhammer Online add-on and the talisman calculator, I was consciously trying to act more like a digital native than a digital immigrant, to see how well it worked. It worked out better than I expected, but not enough to convince me that the digital native approach is better than the digital immigrant approach.
I've seen the fizz buzz test mentioned a lot lately, and had assumed that it was referring to the somewhat more challenging version I knew. Do people really have that much trouble with this simple version?
I'm actually not that surprised people have trouble with it. Even in my CS program at a decent university, I encountered many students that could still not handle simple programming tasks in advanced courses, and were relying on the internet, cheating and heavily leaning on TAs to get through.
Then again, my experience is with students at CMU and developers in Silicon Valley.
The stat still seems kind of far out though...
To salvage my dignity, here's the Haskell solution:
main = mapM_ putStrLn $ map fizzbuzz [1..100]
where fizzbuzz n | n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 3 == 0 = "Fizz"
| n `mod` 5 == 0 = "Buzz"
| otherwise = show nAlso I went to the [calculator challenge](http://www.trybloc.com/courses/calculator#/1) and noticed this
# add(4, 2) => 8
def add(x, y)
end
Needless to say the comment should read `=> 6`.Also, since we are all geeks, here's my Clojure version
(defn buzziffy [a b x]
(cond (and (zero? (mod x a)) (zero? (mod x b))) "FizzBuzz"
(zero? (mod x a)) "Fizz"
(zero? (mod x b)) "Buzz"
:else x))
(println (apply str (map #(str (buzziffy 3 5 %) "\n")
(range 1 100))))and got this :
"Nice job, you beat the FizzBuzz test! If you believe in the rumors, you are officially better than 99.5% of programmers."
--
I'm new to ruby, is there any way to "optimize" that code? - Edit : I don't know how to format code for HN.
Your solution is pretty close, and honestly, is decent enough. There are some idiomatic shortcuts you could take, though; ranges are objects and expose #each, you can use postfix conditionals, and you can use % instead of #modulo.
Here's how I'd do it.
(1..100).each do |i|
print i unless i % 3 == 0 or i % 5 == 0
print "Fizz" if i % 3 == 0
print "Buzz" if i % 5 == 0
print "\n"
end
(print is like puts, but doesn't append the newline, letting me construct the line piecewise).This might actually run a wee bit slower than your solution, because all three conditions are evaluated per run, but it's less code and easier to read. Good lead-in to a discussion about code clarity vs optimization.
(Yes, it's ugly. But it works!)
(1..100).each { |i| puts [i % 3 == 0 ? "Fizz" : nil, i % 5 == 0 ? "Buzz" : nil].join.first || i }Apparently, assigning the result of a comparison to a literal breaks this demo.
Edit: Also, bonus points if you can elaborate on why you think my second solution really does work correctly, despite the apparent bug. :)
Output:
<title>Application Error</title>
<iframe src="https://s3.amazonaws.com/heroku_pages/error.html">
<p>Application Error</p>
</iframe>Can we have a title that isn't strongly reminiscent of facebook spam?
(1..100) do |i|
if i * 2 == 8 and i + 1 == 5
puts "foo"
elsif i + 3 == 9
puts "bar"
else
puts i
end
end
Not the solution, but all the syntax you'll need in there.> Oops, looks like we found 0 errors in your code. Try again and click "Grade it!"
So I guess that means I passed?
(After reloading the page it seemed to work better)
You would be shocked at some of the people out there who call themselves programmers, and even more shocked at how many of these people have supported themselves for years.
out = [
["FizzBuzz", "Fizz", "Fizz", "Fizz", "Fizz"],
["Buzz"],
["Buzz"]
]
(1..100).each do | ii |
puts out[ii % 3][ii % 5] || ii
end
Thanks!PS: i am assuming this is the fastest you can do in ruby (but I suspect that blocks are expensive alas)
PS2: passing the test is interesting, having a look at the good and/or best answers would be a decent award for having tried in the first time.
I don't know ruby, so I didn't make a response, but these things should be spelled out.
I don't know Ruby either, so I looked up the syntax for a for loop, guessed at the conditional (elseif vs elsif vs else if) and used puts for output... and that was all it takes to pass the little test.