top | item 18356685

Fifty Fizzbuzzes

143 points| Jakob | 7 years ago |vihart.com

57 comments

order
[+] codereflection|7 years ago|reply
I like this idea. I find it fascinating how many developers I've interviewed who still have not come across FizzBuzz, and how many of those cannot solve the problem. Some of the more interesting interviews I've given though have been with people who know the FizzBuzz problem, where I ask them to make the worst possible version of it that they can. It turns out to be more difficult than they think it will be, and a deeper thinking and communicating exercise than solving the simple FizzBuzz problem itself.
[+] jtolmar|7 years ago|reply

  let tokens = [];
  tokens.push("Fizz", "Buzz", "Fizzbuzz");
  for (let i = 0; i <= 100; i++) { tokens.push(i); }

  function generateFizzBuzz(n) {
    if (n > 100) { return [[]]; }
    let result = [];
    for (let token of tokens) {
      for (let tail of generateFizzBuzz(n+1)) {
        tail.splice(0, 0, token);
        result.push(tail);
      }
    }
    return result;
  }

  let candidates = generateFizzBuzz(1);
  while (candidates.length > 1) {
    let candidateIndex = Math.floor(Math.random() * candidates.length);
    let candidate = candidates[candidateIndex];
    let checkIndex = Math.floor(Math.random() * 100);
    let expected = checkIndex % 3 == 0
      ? (checkIndex % 5 == 0 ? "Fizzbuzz" : "Fizz")
      : (checkIndex % 5 == 0 ? "Buzz" : checkIndex);
    if (candidate[checkIndex] !== expected) {
      candidates.splice(candidateIndex, 1);
    }
  }

  return candidates[0];
[+] a1369209993|7 years ago|reply
> the worst possible version of it that they can

Based on a true story, sadly:

  #! /usr/bin/tail -n+2
  1
  2
  Fizz
  4
  Buzz
  [...]
  98
  Fizz
  Buzz
On the other hand, there's also ones like:

  let{a="ssfsbfssfbsfssX"++a;x 's'=show;x 'f'=const"Fizz";x 'b'=const"Buzz";x 'X'=const"FizzBuzz"}in putStrLn$concat$map(\(i,f)->x f i++"\n")(zip[1..100]a)
(because who needs divisibility tests, right?)
[+] Waterluvian|7 years ago|reply
Can't solve because they weren't actually programmers or more because of deer in headlights?
[+] octavefb|7 years ago|reply
octave

  a=[1:100];
  b=strcat({'','fizz'}([1+!mod(a,3)]),{'','buzz'}([1+!mod(a,5)]));
  a=num2cell(int16(cellfun(@isempty,b)).*a);
  b(cellfun(@isempty,b))=0;
  strcat(b,a)
[+] romwell|7 years ago|reply
Vi's site is down from the onslaught.

In the meantime, enjoy the ever-classic Fizz Buzz Enterprise Edition:

https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris...

[+] toxik|7 years ago|reply
I opened the link thinking ”hah, what a gag!” But then I thought it’d be funny to try to find the core fizzbuzz logic. Turns out that it hits way too close to home, I just got really annoyed. Not sure what I expected.
[+] sincerely|7 years ago|reply
I feel like everyone leaving a fizzbuzz solution in the comments of this post is missing the point of this article...
[+] jpindar|7 years ago|reply
Fizzbuzz does that to some people. It's a mind-control meme, and a very powerful one.
[+] emmelaich|7 years ago|reply
I didn't know that fizzbuzz was a children's game.

That explains a LOT. As in most descriptions of it are annoyingly ambiguous.

Whereas if you knew the game, you know it already.

[+] thrmsforbfast|7 years ago|reply
> As in most descriptions of it are annoyingly ambiguous.

My very first interviewer (intentionally) didn't spec fizzbuzz correctly. The real test was whether the candidate listened to the customer's/lead engineer's spec instead of jumping to conclusions.

Fortunately, I was just entering college and hadn't heard of fizzbuzz before. I passed the "test" but for the wrong reason.

[+] tne|7 years ago|reply

  #include <stdlib.h>
  #include <stdio.h>
  
  static const char str[] = "fizzbuzz";
  
  static inline void out(int i, size_t offset, size_t sz)
  {
  	if (sz) {
  		fwrite(str + offset, sz, 1, stdout);
  	} else {
  		fprintf(stdout, "%d", i);
  	}
  	putchar('\n');
  }
  
  int main(void)
  {
  	for (int i = 1; i <= 100; i++) {
  		int a = i % 3 == 0;
  		int b = i % 5 == 0;
  		out(i, 4*((a^b)&b), 4*(a+b));
  	}
  	exit(0);
  }
There's a way to remove that conditional, too. Probably.
[+] 333c|7 years ago|reply
This is called out in the post, but 32 is actually a really cool rhythmic (and accurate) fizzbuzz.