
Fizz Buzz in Tensorflow (2016) - sametmax
http://joelgrus.com/2016/05/23/fizz-buzz-in-tensorflow/
======
vpmpaul
I know people consider it condescending if they are actually a competent
programmer. However as a person that has done hiring I can't tell you how many
people have a CS degree that they supposedly attended school and passed(as in
their credentials check out) but can't even do the FizzBuzz test.

Once time for fun I used one of those online code collaboration tools. I had a
couple of "programmers" that were still actively working on the Fizzbuzz
question 4 hours later. I went to lunch and forgot about it after I came back
until the end of the day and realized there were still people working....

~~~
romaniv
It _is_ condescending. Asking people to do FizzBuzz indicates that you expect
a large portion of your candidates to be completely clueless. That's not a
good message to send.

Think about the following. FizzBuzz filters out some extreme cases of
incompetence, but tells you nothing of value about the people who passed. You
can use the same chunk of interview time to ask a more challenging question
that will still filter out the extremely incompetent, but also give you some
level of confidence in the people who passed. So why would you prefer to ask a
question that gives you less useful information?

It's not unlike a sorted array search algorithm. To maximize your efficiency
you start in the middle, not at some arbitrarily chosen low point.

~~~
dictum
Maybe I'm missing something or it's a personality trait, but I think if you're
an expert, being asked to do a FizzBuzz isn't condescending: it's an
opportunity to play in speedrun mode.

I deeply dislike the job interview culture and whiteboard trickery, but I'm
also wary of people who see themselves above mundane requests. That's how you
hire someone who leaves code undocumented ("who needs it? I can understand it
perfectly"), adopts clever but inscrutable techniques, etc.

~~~
romaniv
_> I'm also wary of people who see themselves above mundane requests._

Let me give you an analogy from another domain.

"My company requires excellent communication and writing skills, so would you
mind talking a 5h grade spelling and grammar test?"

~~~
dsacco
_> "My company requires excellent communication and writing skills, so would
you mind talking a 5h grade spelling and grammar test?"_

That's not comparable. A total whiteboard interview might take several hours,
but it increases significantly in complexity from basic FizzBuzz.

A more comparable analogy would be asking them to merely edit a few paragraphs
for grammatical mistakes, or something similarly trivial in whatever domain
you'd like that should take fewer than 10 minutes if the person has literally
any meaningful facility with the work.

Like the person you're responding to, I'll say this: I deeply dislike
whiteboard coding culture. But of the things I dislike about whiteboard
coding, I dislike dead simple sanity checks the least.

~~~
GetThreadCtxt
I think they meant to say "5th", not "5h" as in "5 hour".

------
ericfrederich
This reminds me of enterprise fizz buzz. If you haven't seen it, it's great.

[https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...](https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition)

~~~
romaniv
This legitimately looks like some legacy apps I had to deal with in real life.
The language and the framework of choice were different, but the principle was
the same.

------
fatjokes
If this actually happened the only signal I would get from this interview is
that the candidate is a egotistical asshole. No hire.

\- It's always good to _not_ assume that the candidate is familiar with a
given question, no matter how well known it may be. Maybe they're from a
different country, maybe they're from a different industry altogether making a
transition.

\- I bet if the question was difficult or complex the interviewer would say
"omg that's not realistic, would never use this in real job".

Then again, I'm also in the minority of HN users who like coding/problem
solving interviews so what do I know.

~~~
bllguo
Seems obvious to me that the guy wasn't interested in the job when he was told
to do fizzbuzz, hence the stunt. Being "egotistical" was kinda the whole
point.

------
razodactyl
This was fun, I did a live demo at work a few months ago on this, got stuck on
getting the model to predict with enough accuracy to be useful then came
across this post, my activation function was slightly off.

Most people understand Fizz Buzz, building up and solving the problem with a
neural network shows that a bit of ML code can solve the problem without being
programmed explicitly to do so, quite a good example.

~~~
skrebbel
> ML code can solve the problem without being programmed explicitly to do so

Umm, no. He implemented all of Fizz Buzz except the loop in his
"fizz_buzz_encode" function. Totally explicit.

~~~
nl
That’s the joke!

He implements it explicitly but only to generate training data.

------
geraldbauer
Love the write-up. FYI: I've published a (free online) book(let) about
FizzBuzz titled "FizzBuzz by Example - There's More Than One Way To Do It" [1]
in the Yuki & Moto Press Bookshelf series. [1]
[http://yukimotopress.github.io/fizzbuzz](http://yukimotopress.github.io/fizzbuzz)

PS: My favorite FizzBuzz version - the gold standard with constant lookup:

def fizzbuzz [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"] end

~~~
buzer
Seems like all of the examples (short of precomputed table) use modulo. There
are couple of other ways to deal with it as well. Two that I came up with at
one point:

1\. Create your own mod function (you can most likely leave out dealing with
negatives and overflows as long as you just point that out). Given dividend X
and divisor Y, while X>=Y: X=X-Y and finally return X.

2\. Take human approach and convert the number to string. Skip 0/print it
depending on definition. If the last character is 0 or 5, it's dividable by 5.
For dividable by 3, you loop thru the string and sum the numbers. Repeat until
the length of string is 1. Now compare it to 3, 6 and 9 (you can convert it
back to number or just compare the string of those). If it's true, it's
dividable by 3.

Not nearly as optimized as mod-based solution or precomputed tables, but they
show it's possible to solve it even if you are not familiar with modulo (it's
not very commonly used in some higher level languages).

~~~
geraldbauer
Good point. Another example without modulo in the FizzBuzz by Example book is
using cycle e.g.:

def fizzbuzz

    
    
      fizzy = [nil, nil, :Fizz].cycle
      buzzy = [nil, nil, nil, nil, :Buzz].cycle
    
      (1..100).map do |n|
         "#{fizzy.next}#{buzzy.next}"[/.+/] || n
      end

end

~~~
arcticbull
Hah, you beat me to it, I just wrote this up in Rust.

    
    
        fn main() {
            let mut seq_mod_3 = [false,false,true]
                .into_iter()
                .cycle();
            let mut seq_mod_5 = [false,false,false,false,true]
                .into_iter()
                .cycle();
            for i in 1..101 {
                let divisible_by_3 = *seq_mod_3.next().unwrap();
                let divisible_by_5 = *seq_mod_5.next().unwrap();
                
                match (divisible_by_3, divisible_by_5) {
                    (true , true ) => println!("fizzbuzz"),
                    (true , false) => println!("fizz"),
                    (false, true ) => println!("buzz"),
                    (false, false) => println!("{}", i),
                }
            }
        }

------
darepublic
Everytime I am reminded of these fizz buzz stories I can't help but bust out
console and write a quick implementation of this to reassure myself I'm not
utterly incompetent. I guess I have a bad case of impostor syndrome.

~~~
thenoblebuffalo
You're not the only one.

------
rakoo
I thought aphyr's version was the original one, but dates don't match:
[https://aphyr.com/posts/340-acing-the-technical-
interview](https://aphyr.com/posts/340-acing-the-technical-interview)

I'm sure I've seen another version already.

------
kahlonel
Here is a C version of fizzbuzz if anyone has an interview tomorrow:

    
    
      #include <stdio.h>
      int main(void)
        {
          int i = 0;
          for(i=1; i<=100; i++)
          {
            unsigned int f = 0;
            if(i % 3 == 0)
            {
              f = 1;
            }
            if(i % 5 == 0)
            {
              f |= 2;
            }
            if(f == 0)
            {
             printf("%d\n",i);
            }
            else if(f == 1)
            {
              puts("fizz");
            }
            else if(f == 2)
            {
              puts("buzz");
            }
            else if(f == 3)
            {
              puts("fizzbuzz");
            }
          }
          return 0;
      }

~~~
msla
This is the shortest non-clever version I can come up with:

    
    
        #include <stdio.h>
    
        int main(void)
        {
            int i;
    
            for (i = 1; i <= 100; i++) {
                if ((i % 3) == 0) {
                    printf("Fizz");
                }
    
                if ((i % 5) == 0) {
                    printf("Buzz");
                }
    
                if ((i % 3) && (i % 5)) {
                    printf("%d", i);
                }
    
                puts("");
            }
    
            return 0;
        }

~~~
geraldbauer
Here's the short ruby version from code golfing in 54 bytes:

def fizzbuzz (1..100).map{|n|["%sBuzz"%x=["Fizz"][n%3]][n%5]||x||n} end

puts fizzbuzz.join

------
zeristor
This is excellent.

Looks like this was first posted two years ago, and has been posted a number
of times since; but I never noticed any of them.

~~~
sametmax
Me neither. It reminds me a lot of
[http://discuss.joelonsoftware.com/default.asp?joel.3.219431....](http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12)

The data is conclusive : there is a strong correlation between those articles
and the name joel.

~~~
inglor
This one is just on Joel's site though - it was written by
[http://www.benjismith.net/](http://www.benjismith.net/)

------
technofiend
I'm still waiting for some wag to just put a python module on github that
solves fizzbuzz and import it in his interview. ;-)

------
netheril96
Maybe adding `layer_norm` would help training of the fully connected network.
Especially since the input has a large range.

------
jxub
It seems we can already train a neural network to outperform an average JavaEE
programmer. For many of them, his is the start of an era of gloom.

------
anc84
(2016)

------
Scea91
Nice. He built an overcomplicated algorithm that is wrong. FizzBuzz was
created to also identify this kind of candidates apart from those that can't
program at all.

Nevertheless, if I was an interviewer I would have several questions that were
unanswered in this iamverysmart writeup:

1) Why did you choose to encode 1 dimensional input as 10 dimensional input
with the binary encoding?

2) Do you think that one source of error could be using different underlying
distribution for the training and testing dataset? You are basically trying to
extrapolate the training dataset.

3) Since this problem can be easily visualized, plot the learned function.

~~~
Q6T46nT668w6i3m
It’s a joke. :-)

Regarding your second point, it would’ve been less funny if they didn’t
overfit. :D

