TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Can you beat 99% of programmers? Try the FizzBuzz challenge

19 pointsby jmtameover 13 years ago

19 comments

kbobover 13 years ago
Suppose FizzBuzz were a perfect proxy for programming ability. Some programmers are FizzBuzz-capable, and the rest are FizzBuzz-failures. Members of both groups interview for jobs. A FB-capable programmer goes on three interviews, gets an offer, and works at his job for a couple of years. A FB-failure goes on three interviews, gets no offers (or if he gets an offer, gets fired shortly after), and goes on three more interviews the next week.<p>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.
gacbaover 13 years ago
You may laugh at the utter simplicity of this test, but I administer this several times a week when I interview candidates.<p>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.
评论 #3354296 未加载
评论 #3354256 未加载
seagaiaover 13 years ago
How is it possible that 99% of programmers fail this? What <i>qualifies</i> as a programmer? (Where is that statistic even from...) Does that mean someone with a CS degree? Are the failures stemming from not knowing the general concept, or is it just lack of familiarity with any language (I suppose both are quite horrifying)
评论 #3354302 未加载
评论 #3355764 未加载
评论 #3354787 未加载
absconditusover 13 years ago
I do not know Ruby, but I wanted to see what happened when I clicked next. Without writing any code I clicked "Grade it!" This provided me with the unit test output. I looked at it for a moment and then guessed at a solution that might work using copy and paste and based on the comment in the code. Here it is:<p>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"]<p>I passed.
评论 #3354498 未加载
评论 #3354364 未加载
评论 #3355908 未加载
Turing_Machineover 13 years ago
Those rules are somewhat different to the ones I learned in HS. My teacher's version mapped buzz to multiples of <i>seven</i>, not three, and also required you to use fizz if there was a five anywhere in the number, and buzz for any number containing a seven. Thus, 17,27,37.. were buzzes, as were 70..79, 170..179, and so on. 57 and 75 were both fizz-buzz by that rule. Numbers with multiple 5s had to use the appropriate number of fizzes, and numbers with multiple 7s had to use the appropriate number of buzzes. Thus, 757 would be "buzz-fizz-buzz".<p>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?
评论 #3354389 未加载
评论 #3354287 未加载
jchonphoenixover 13 years ago
I don't know. I've met a ton of programmers and yes have seen people fail at fizzbuzz. But 99%? that number seems a little high.<p>Then again, my experience is with students at CMU and developers in Silicon Valley.<p>The stat still seems kind of far out though...
thurnover 13 years ago
It took me a while to figure out why mine wasn't working because I wrote [1..100], which is a one-element array with a range in it. Too much Haskell, I guess.<p>To salvage my dignity, here's the Haskell solution:<p><pre><code> 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 n</code></pre>
jrmoranover 13 years ago
This might be a little offtopic, but I would advise against leaving `console.log` calls in production code, even when there are checks to prevent calling it when the console object is undefined. Commenting those lines out would do the trick, since minification can remove them.<p>Also I went to the [calculator challenge](<a href="http://www.trybloc.com/courses/calculator#/1" rel="nofollow">http://www.trybloc.com/courses/calculator#/1</a>) and noticed this<p><pre><code> # add(4, 2) =&#62; 8 def add(x, y) end </code></pre> Needless to say the comment should read `=&#62; 6`.<p>Also, since we are all geeks, here's my Clojure version<p><pre><code> (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))))</code></pre>
lowglowover 13 years ago
I passed with this : class FizzBuzz def self.run for i in (1..100) if((i.modulo(3)==0) &#38;&#38; (i.modulo(5)==0)) puts "FizzBuzz" elsif(i.modulo(3)==0) puts "Fizz" elsif (i.modulo(5)==0) puts "Buzz" else puts i end end end end<p>and got this :<p>"Nice job, you beat the FizzBuzz test! If you believe in the rumors, you are officially better than 99.5% of programmers."<p>--<p>I'm new to ruby, is there any way to "optimize" that code? - Edit : I don't know how to format code for HN.
评论 #3354406 未加载
评论 #3354400 未加载
chealdover 13 years ago
The fun thing about these sorts of tests if in figuring out how to do it a little bit differently. For example, I wanted to see if I could do it with a true one-liner (ie, no semicolons) with piecewise construction (ie, no nested ternaries). Here's my solution.<p>(Yes, it's ugly. But it works!)<p><pre><code> (1..100).each { |i| puts [i % 3 == 0 ? "Fizz" : nil, i % 5 == 0 ? "Buzz" : nil].join.first || i }</code></pre>
评论 #3371559 未加载
评论 #3371370 未加载
dlitzover 13 years ago
My initial solution works in real Ruby, but it didn't work in this demo: <a href="http://imgur.com/YBYwN" rel="nofollow">http://imgur.com/YBYwN</a><p>Apparently, assigning the result of a comparison to a literal breaks this demo.<p>Edit: Also, bonus points if you can elaborate on why you think my second solution really does work correctly, despite the apparent bug. :)
garethspriceover 13 years ago
Submitted a (locally tested as working) program, got "0 errors found" and an error message. The ironing is delicious.<p>Output:<p><pre><code> &#60;title&#62;Application Error&#60;/title&#62; &#60;iframe src="https://s3.amazonaws.com/heroku_pages/error.html"&#62; &#60;p&#62;Application Error&#60;/p&#62; &#60;/iframe&#62;</code></pre>
redthrowawayover 13 years ago
&#62;Can you beat 99% of programmers?<p>Can we have a title that isn't strongly reminiscent of facebook spam?
mrchessover 13 years ago
Some ruby code for those non-rubyists:<p><pre><code> (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 </code></pre> Not the solution, but all the syntax you'll need in there.
评论 #3354251 未加载
barrybeover 13 years ago
I tried just hitting "Grade it" and got this reply:<p>&#62; Oops, looks like we found 0 errors in your code. Try again and click "Grade it!"<p>So I guess that means I passed?<p>(After reloading the page it seemed to work better)
ontehfritzover 13 years ago
I am happy I passed, I was worried I suck. What a great day! :)
switzover 13 years ago
Never really touched ruby before and did it without any help other than checking google for some ruby syntax. I highly doubt 99% of "programmers" can't do this.
评论 #3354404 未加载
jhrobertover 13 years ago
<p><pre><code> out = [ ["FizzBuzz", "Fizz", "Fizz", "Fizz", "Fizz"], ["Buzz"], ["Buzz"] ] (1..100).each do | ii | puts out[ii % 3][ii % 5] || ii end </code></pre> Thanks!<p>PS: i am assuming this is the fastest you can do in ruby (but I suspect that blocks are expensive alas)<p>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.
sukuriantover 13 years ago
The challenge is insufficiently specified. What sort of spacing am I supposed to put between the number, fizz/buzz, and the next answer? A space? A colon and a space? Does it matter?<p>I don't know ruby, so I didn't make a response, but these things should be spelled out.
评论 #3354252 未加载
评论 #3354249 未加载
评论 #3354277 未加载
评论 #3354331 未加载