News for hackers. Stuff that matters.
Re:Branch-free FizzBuzz in Assembly by Terretta on Saturday January 03, 2015 @06:11PM (#8832424)
Random fizzy buzzy observation:

Always a little baffled when I see three cases (fizz, buzz, and fizzbuzz) rather than both fizz and buzz handled well such that modulo 15 gets a fizz and a buzz.

The normal fizz buzz is divisible by 3 gives a fizz, divisible by 5 gives a buzz. It so happens that 15 is both, so should exhibit both. But as I see it, handling the 15 right should be emergent behavior of a correctly written fizz and correctly written buzz, not a separate test case.

But what if the business requirement changes that divisible by 4 gives a fizz and divisible by 5 gives a buzz? Now the "3 cases" programmer doesn't just change 3 to 4, and correctly see fizzbuzz emerge on divisible by 20, he's still got a spurious fizzbuzz on the divisible by 15 case. He's got to remember this case, do some manual computation, and change his 15 to 20. Structuring the code in such a way the programmer has to track all ancillary implications of one business rule change seems a recipe for disaster.

So I consider as broken all code that explicitly prints "fizzbuzz" with a third conditional.

Re:Branch-free FizzBuzz in Assembly by blt on Saturday January 03, 2015 @06:34PM (#8832486)
I consider as broken all interviewers who extrapolate a candidate's decision on this issue to real-world "business requirement changes."
Re:Branch-free FizzBuzz in Assembly by austinz on Saturday January 03, 2015 @07:45PM (#8832700)
This is one of the worst parts of technical interviews.

"Take 10 minutes to write out code for this simple, abstract, poorly defined problem. Okay, now I'm going to ding you because you didn't write it using the same mindset you'd use when writing a large application tailored towards a specific domain and given a detailed set of constraints."

Maybe we should call this sort of cargo-cultism Schroedinger interviews. If you make your code simple and easily readable in order to solve the problem in the most intelligible way, you get penalized for not making your solution scalable/easily adaptable to "business requirement changes". If you structure your code so that it can easily be extended or modified you get penalized for YAGNI and premature optimization. Of course, exactly what the interviewer is looking for is never actually specified as part of the problem statement, because it exists in a state of quantum superposition until right before the interviewer decides whether or not he or she likes your physical appearance/sense of humor/"cultural fit".

Re:Branch-free FizzBuzz in Assembly by jnmuel on Saturday January 03, 2015 @08:03PM (#8832738)
It's interesting that you can't imagine code that explicitly prints fizzbuzz with a third conditional. It's as simple as this:

for i in fizzbuzz range fizz = i % 3 == 0 buzz = i % 5 == 0 if fizz and buzz print fizzbuzz else if fizz print fizz else if buzz pritn buzz else print i

If you want to see how they react to changing requirements, then ask them to change it. It's stupid to look at someone's answer to a toy problem and treat it like it's production code.

Re:Branch-free FizzBuzz in Assembly by jacquesm on Saturday January 03, 2015 @08:32PM (#8832798)
> It's stupid to look at someone's answer to a toy problem and treat it like it's production code.

There are no toy problems in interviews. The way you approach a toy problem says everything about how you would write production code according to the 'how to hire programmers' manual v 3.3.

Obviously that's not how it should be but if you're going to places where they think 'fizzbuzz' is going to weed out the ones they don't want then you'd better be showing off your capabilities.

Re:Branch-free FizzBuzz in Assembly by eckzow on Saturday January 03, 2015 @06:23PM (#8832457)
A good interview exercise (not necessarily vouching for fizzbuzz here) allows depth of discussion past the initial answer. It is in this discussion stage where I spend the majority of my time with a good candidate.

In such a discussion we might talk about whether it makes sense to make the /15 case "fall out" naturally. Using your example, there's an implicit assumption that the requirements would change to {4: fizz, 5: buzz, 20: fizzbuzz}, but in real life I've found things have the annoying tendency to change to, e.g. {4: fizz, 5: buzz, 15: fizzbuzz}.

Re:Branch-free FizzBuzz in Assembly by lxe on Saturday January 03, 2015 @07:03PM (#8832564)
How would you handle a case where it's not divisible by either? We need to handle a conditional that evaluates to true when the number is not divisible by either 3 or 5:

    #include <stdio.h>
    void main()
    {
        unsigned int i, is3, is5;

        for (i = 1; i < 101; i++)
        {
            is3 = i % 3 == 0;
            is5 = i % 5 == 0;

            // If both are true,
            // FizzBuzz gets printed
            if (is3) printf("Fizz");
            if (is5) printf("Buzz");

            // Still need to handle
            // the raw number though
            if (!is3 && !is5) printf("%i", i);

            printf("\n");
        }
    }
Re:Branch-free FizzBuzz in Assembly by ajbonkoski on Saturday January 03, 2015 @09:51PM (#8832980)
Well, I'm always a little baffled when I see such great techniques for rejecting potentially awesome candidates.

Why overthink a toy example? The only purpose served by fizzbuzz is to weed out people who can't write any code.

If you want to know anything else, ask a more relevant technical question. It's downright lazy to simply jump to conclusions, and as a result you end up hiring the wrong person.

Re:Branch-free FizzBuzz in Assembly by Terretta on Saturday January 03, 2015 @10:09PM (#8833055)
To be clear, I'm not referring to candidates. I'm referring to the questioners/interviewers suggesting this approach.
Re:Branch-free FizzBuzz in Assembly by OMGWTF on Saturday January 03, 2015 @09:59PM (#8833018)
Here I have an explicit "FizzBuzz" but only 2 checks. Do you consider this also broken?

   python3 -c '
     for i in range(1,20):
       print([i,"Fizz","Buzz","FizzBuzz"][int(i%3==0) + 2*int(i%5==0)])'
Re:Branch-free FizzBuzz in Assembly by rertrree on Saturday January 03, 2015 @06:32PM (#8832483)
You assumption that a separate case that prints fizzbuzz will break the code is not correct. With correct code, you get three if checks either way, but the readability is higher, code complexity is lower and cases are nicely separated.

  bool is3 = i%3 == 0 ;
  bool is5 = i%5 == 0 ;	
  
  if( is3 && is5 )
  {
  	printf("fizzbuzz\n") ;
  }
  else if( is3 )
  {
  	printf("fizz\n") ;
  }
  else if( is5 )
  {
  	printf("buzz\n") ;
  }
  else
  {
  	printf("%d\n" , i ) ;
  }
Re:Branch-free FizzBuzz in Assembly by Terretta on Saturday January 03, 2015 @10:13PM (#8833082)
And what if there's a is7 woof criteria as well? You going to add another set of special cases for fizzwoof, buzzwoof, and fizzbuzzwoof?

You see the problem, versus just doing fizz, buzz, and woof distinctly?

Re:Branch-free FizzBuzz in Assembly by rertrree on Sunday January 04, 2015 @06:51AM (#8834157)
It is not logical to subvert your code to adhere to some imaginary future requirements.
Re:Branch-free FizzBuzz in Assembly by dthal on Saturday January 03, 2015 @10:12PM (#8833072)
>But what if the business requirement changes...

Are you kidding? This is FizzBuzz! It is just a quick check to verify that an applicant is even a programmer at all. That's all it is. There is nothing else there.

Re:Branch-free FizzBuzz in Assembly by ky3 on Sunday January 04, 2015 @05:05AM (#8833985)
Depends on how the task is communicated and what's implied.

Also recognize that there are excellent programmers who take to heart Dijkstra's dictum to "Always design your programs as a member of a whole family of programs, including those that are likely to succeed it."

Has AmaTwitBookGooSoft ever asked for a generalization of FizzBuzz? I wouldn't put it pass them.

Re:Branch-free FizzBuzz in Assembly by hueving on Saturday January 03, 2015 @08:34PM (#8832807)
I don't think you know what the word 'broken' means. You sound like the type of person that would make code as terse as possible with all kinds of traps for future maintainers.
Re:Branch-free FizzBuzz in Assembly by Terretta on Saturday January 03, 2015 @10:11PM (#8833067)
Fizz on /3, Buzz on /5, is the actual requirement. Shortcutting /3 & /5 with a hard coded /15 that has to be edited if /3 or /5 changes, that's the trap.