Live data from Hacker News

Fizzbuzz, Interviews, And Overthinking

dave.fayr.am

61–70 of 115 posts

Re: Fizzbuzz, Interviews, And Overthinking

#62
post #14

I recently challenged people to codegolf fizzbuzz ( http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-wil... ) The Haskell solution was really cool: [max(show x)(concat[n|(f,n) This is much simpler and it looks easier to extend as well.

Python: under 90 characters and no explicits if statements

    for i in range(100):print ''.join([s*(i%m==0)for m,s in[(3,"Fizz"),(5,"Buzz")]])or i

Re: Fizzbuzz, Interviews, And Overthinking

#63
post #57

Earlier quoted context omitted.

It's a different kind of pressure though.

Explain. If you can't produce a god damned string reversal function in 10 minutes, why would I believe you can produce a bug fix to your own, complex code in a few hours? That's what interviewers are considering: the interview is a proxy to see if you can come close to the requirements of the job.

Can you get in the zone with people watching and judging you intently? If you can't get into the zone with only a couple of people watching you, how will you get into the zone when the whole company is depending on your bug fix?

The pressure of judgement is not the same as the work pressure of a stressful scenario. It's why public speaking and job interviews are among the most feared activities people go through, but people consider meetings boring and mundane.

Re: Fizzbuzz, Interviews, And Overthinking

#64
post #14

I recently challenged people to codegolf fizzbuzz ( http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-wil... ) The Haskell solution was really cool: [max(show x)(concat[n|(f,n) This is much simpler and it looks easier to extend as well.

Perl:

    # 97 characters
    for my $i (1..100) { say join("", map { {3=>'fizz',5=>'buzz'}->{$_} unless $i % $_ } 3,5) || $i }

    # 81, if those pesky spaces are removed
    for my$i(1..100){say join("",map{{3=>'fizz',5=>'buzz'}->{$_}unless$i%$_}3,5)||$i}

Re: Fizzbuzz, Interviews, And Overthinking

#65
post #18

I agree that Fizzbuzz can be a more interesting example of how to write code without repetition. While the author suggests that languages such as Haskell provide a unique advantage, the deciding question seems to be the availability of pre-built abstractions. Consider the following solution in Python: for i in xrange(1,101): print (('' if i%3 else 'Fizz')+('' if i%5 else 'Buzz')) or i or the even more general: mappin…

My "I'm going to hell, but that's okay" C version: #include #include #define when(mod, msg) do { if((i mod) == 0) { fputs(#msg, stdout); *hit = true; } } while(0) #define through ; i Super extensible!

My "I'm going to hell" C++ version: https://gist.github.com/3838042

Or, since github's being broken right now:

    #include 
    #include 
    
    template 
    class Noisemaker {
    private:
        union {
            uint32_t noise_as_int;
            char noise_as_cstr[5];
        };
    public:
        Noisemaker() {
            noise_as_int = htonl(noise);
            noise_as_cstr[4] = '\0';
        }
        bool operator()(std::ostream &out, int i) {
            if(i % multiples_of == 0) {
                out 
    class NoisemakerPair {
    private:
        Noise1 noise1;
        Noise2 noise2;
    public:
        bool operator()(std::ostream &out, int i) {
            bool matched = false;
            matched |= noise1(out, i);
            matched |= noise2(out, i);
            return matched;
        }
    };
    
    int main(int argc, char *argv[]) {
        NoisemakerPair, Noisemaker > fizzbuzz;
        for(int i=1;i
You can nest NoisemakerPairs to add more Noisemakers. But good luck with strings longer than 4 characters… for some absurd reason, string literals can't be template parameters. Go figure.

Re: Fizzbuzz, Interviews, And Overthinking

#66
post #14

I recently challenged people to codegolf fizzbuzz ( http://swizec.com/blog/fizzbuzz-without-ifs-in-90-char-i-wil... ) The Haskell solution was really cool: [max(show x)(concat[n|(f,n) This is much simpler and it looks easier to extend as well.

That max looks suspicious. It (ab)uses the fact that the strings "Fizz", "Buzz" compare higher than any printed integer. If you try to extend it to (7, " Bazz") it fails, because " " comes before digits.

Re: Fizzbuzz, Interviews, And Overthinking

#67
This seems overcomplicated. Why wrap String (which is already a monoid) inside Maybe? You can just use concat; if the result is the empty string then print the number. If you want it to work for any monoid, then use mconcat, and test for equality to mempty.

And why introduce monad comprehensions if you're just introducing monoids?

Re: Fizzbuzz, Interviews, And Overthinking

#68
post #51

Earlier quoted context omitted.

Firstly, the ruby code was produced (admittedly from my memory) from someone on the tail end of a 5 hour interview process, of which I was not the first technical interviewer. It is exceptional in that context. Secondly, I feel like you (and aristus in his/her code snippet above) missed the secondary point of my post. Please consider that monoids and Maybe are capturing a higher level pattern in a way that we can com…

I'd like to understand more that second point. How is Maybe not morally equivalent to a conditional? If my ifs were in a separate function that would also work, no?

I had a bit more time to spare and thought I'd give you an example of this. First, let's rewrite fizzbuzz to totally get rid of any mention Maybe; we'll deal with it outside:

    fizzbuzz d i = mconcat (rules  pure i)  pure (d i)
This is written in terms of an Applicative Functor and a Monoid.

And then can just do some haskell things. I've done less golfing here and more type signatures to make the code more approachable.

    {-# LANGUAGE MonadComprehensions, OverlappingInstances, FlexibleInstances#-}
    
    module Main where
    import Control.Applicative
    import Data.Monoid
    import Control.Monad
    import Data.Maybe
    import Data.List
    
    import qualified Data.HashMap.Strict as M
    import System.Environment
    
    -- Let's make it clear what we're working with.
    type Counter = M.HashMap String Integer
    
    -- We want an instance slightly different from the default.
    instance Monoid Counter where
      mempty  = M.empty
      mappend = M.unionWith (+)
    
    factors = [(3, "fizz"), (5, "buzz"), (7, "bazz")]
    
    -- Our rule function is slightly different.
    -- Not unexpected, since our logic has changed.  But we could generalize
    -- this further!
    rules :: [(Integer -> Maybe Counter)]
    rules = [\i -> [M.singleton res 1 | i `rem` fac == 0] | (fac,res)  pure i)  pure (d i)
    
    main = do
      upTo 
And then a typical session:

    ~/P/h/fb-toys > time ./fbg3 10000000
    fromList [("bazz",1428571),("fizz",3333333),("buzz",2000000)]
            3.58 real         3.54 user         0.03 sys
Which is a pretty expensive way to avoid doing algebra, but the point is that we're talking about very high level patterns here for fizzbuzz. Fizzbuzz is probably a bad name here, it's more like mergeOptionalPatterns. I'm willing to bet if I dug a round a bit in parser combinator libraries I could find something that does nearly exactly this.

I confess I had to play with it a bit to get it to play nice with large inputs.

Re: Fizzbuzz, Interviews, And Overthinking

#70
post #57

Earlier quoted context omitted.

It's a different kind of pressure though.

Explain. If you can't produce a god damned string reversal function in 10 minutes, why would I believe you can produce a bug fix to your own, complex code in a few hours? That's what interviewers are considering: the interview is a proxy to see if you can come close to the requirements of the job.

I can program a string reversal with my eyes closed, tied up in chains, upside-down, while holding my breath. Or whatever.

The point is more that for a person with a given skill level, interviews can be stressful and make them perform below that skill level. I try to structure my interviews such that people get more comfortable and I don't ram their head into a metaphorical wall. I get all the same information, but without all those hard feelings.

Post reply on HN