3 ms·
His solution isn't even correct, failing his own test. if !multiple_of_3(i) && !multiple_of_5(i) output = i edit: In order to avoid overwriting output,
by sync 14y ago
His solution isn't even correct, failing his own test.
if !multiple_of_3(i) && !multiple_of_5(i)
output = i
edit: In order to avoid overwriting output, the solution should read...
if !multiple_of_3(i) && !multiple_of_5(i)
output << i
- deleted 14y ago[deleted]
- raganwald 14y agoThat's probably deliberate: he says himself (and I agree with this approach) that the point is to figure out if someone knows how to program at all. Off by one errors, syntax errors, these are not all that relevant. The failures are the ones who stare at the paper blankly, or have no idea that they need a loop, or some other such hopeless approach. If you get "close enough" on paper and pencil under the pressure of an interview, that's great. You can walk through debugging it if you want to see more, but I wouldn't hold even basic logic errors against someone. I certainly admit that when coding on my own I usually get all kinds of stuff wrong that I fix as I write tests and iterate.
- lucisferre 14y agoYeah I've used FizzBuzz myself, and under interview conditions it's actually much harder to implement a correct (for edge cases, etc.) solution in a short time. I always took that into account. However I also added the twist that I wanted the candidates to also write the tests for the program in TDD style. I hoped that this would actually help, by asking the interviewee to follow a repeatable pattern of first expressing what they wanted to code to do and then implementing. We also did ping-pong pairing so I could help write a test, or do an implementation if things were going poorly, to see if that would jump start the candidates brain into the problem. The results were quite frankly a disaster, no better than what Joey found (though I didn't do enough interviews for a statistical sample). Unfortunately my limited observations have lead me to believe that too many developers (particularly in the enterprise environments) are used to working on legacy codebases that they hack and change until the bug they are fixing goes away. Many rarely implement features much, if at all, and the ones they do are heavily broken.
- AccordionGuy 14y ago[Article author here!] Reg is right. The purpose of the FizzBuzz test was to weed out anyone who couldn't program at all and not punish anyone who forgot a semicolon, used = when they should've used == or some similar error. I was completely prepared to discuss the result if someone got close to a working solution. To my surprise, the candidates' solutions were either bang-on or way off. The guy who asked "Do you think I should use a loop?" had to be cut off at the ten-minute mark, and his paper had only the start of a C-syntax for loop and a lot fo scratched-out lines.
- reidrac 14y agoAre you sure? It looks correct to me. If the number is not multiple of 3 or 5, you must output the number. That's what is he doing. He prints output at the end of each iteration, setting it to empty string at the beginning.
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- AgentConundrum 14y agoI've never touched Ruby, so I could be misunderstanding, but I think his code is correct. In each loop, he initializes the output variable to an empty string. If the current integer is not a multiple, he replaces the empty string with the integer. If it is a multiple, he doesn't know which one it is, or if it's a multiple of both, so he appends the text to the existing string variable. At the end of the loop, the string is output. Since the `output = i` line is the only assignment that occurs when that condition (not a multiple of either) is met, it makes sense to just reassign the variable rather than append to it. It might be less flexible, sure, but flexibility isn't required here.
- jeltz 14y agoHis solutions works while your does not. I ran them both in irb. This is because "string << integer" does not do what you think it does. It treats the integer as character code. "" << 0x41 # => "A"
- fhars 14y agoBut the specification is to print the number, not the character with the character code corresponding to the number. So unlike your "correction", his code does what it should. Admittedly, having a variable that contains either an integer or a string is atrocious programming style, but some people do actually like languages that allow that, so it is par for the course in the given situation.
- irahul 14y agoedit: In order to avoid overwriting output, the solution should read... if !multiple_of_3(i) && !multiple_of_5(i) output << i No. Though output << i.to_s will produce the same result, his code is correct. And yours isn't if you do output << i 1. If not a multiple of 3 or 5 or 15, output = i. 2. If a multiple of 3, output << fizz. 3. If a multiple of 5, output << buzz 4. If a multiple of 15, "fizzbuzz". Already covered in 2 and 3 owing to << Apparently people mess up fizzbuzz even when they know it:)
- DougBTX 14y agoThe line before that is output = "", there will never be any output to overwrite by line output = i. Note that this is output for just one line, not the entire output of the procedure.