39
anantdark
259d

# During my first-ever technical interview, the interviewer asked me "Do you know the FizzBuzz problem?" "Uhh, not really." (I was just thinking ok this problem has a name, must be some algorithm problem) "So the problem is basically to give you the numbers 1 to 100, if the number is divisible by 3, print 'Fizz', if divisible by 5, print 'Buzz', if divisible by 3 and 5, print 'FizzBuzz'. For other numbers just print out the number itself." After hearing the problem, I felt so many ideas popping out of my stressed brain. I thought for a bit and said "ok, so if the digit sum of a number is a multiple of 3, then the number is divisible by 3, and if the last digit is either 0 or 5, it's divisible by 5." Then I started to code out my solution until the interviewer said "there's an easier solution. Can you think of it?" This stressed me out even more. I thought for a bit and said "well, starting from 3, keep a counter that records how many iterations are done after 3. When the counter hits 3, that number would be divisible by 3 for sure. Should I try this solution?" The interviewer said "Sure." So I started again. However, I struggled for about another 3min until I realized this solution is a lot harder to implement. The interviewer probably saw my struggle too. This was the point where he stepped in and asked me "Ummmm there's an easy way of solving this. Have you heard of the MODULO OPERATOR?" In sheer embarrassment, I finished the code in 30s. Of course, there was no further question after this, and I felt the need to seriously reevaluate my intelligence afterwards.

• 11
Nah, interview stress is a real thing. You did fine thinking up solutions. Maybe not the perfect solution, but you problem solved.
• 6
Hey, I periodically get scared by graphs just to remember they are easy af, don’t stress yourself too much :)
• 6
That is the most severe problem with the common interview:
It mostly tests social stress tolerance and ability to multitask.

Most coder jobs shouldn't require either.
• 5
I have never used the modulo operator on anything but fizzbuzz
• 3
@Crost I've used it in data segmentation and the dreaded "is this thing even?".

My other half, who is a mechatronic engineer (no I'm not sure what that really means as it seems to be more electrical/electronic than mechanical), made the claim that modulo was useless... And then about two weeks later he needed to use it in VBA formula ðŸ˜‚
• 3
Hey, don't worry, that wasn't what made you look bad. You wrote out the code in 30 seconds after being helped.

Here's what interviewers are used to: https://blog.codinghorror.com/why-c...

I believe, if an interviewer has some understanding he recognises a brain fart as what it is, a brain fart.
• 6
My FizzBuzz implementation is even branchless, except the loop itself. :)
• 5
• 0
@Napstalgic Index-driven lookup tables are pretty much a standard technique for branchless code.
• 3
My brain completely blacked out when I had to solve FizzBuzz in Python, so I just explained what I would have done and they helped me with the implementation. It was probably not a big deal because I got hired anyway.
• 0
@Crost It's also useful when you have to know if a number is odd or even, and to know the nth power of i.
• 2
@Fast-Nop I prefer the bit shift method
Basically :
Int32 mask = 810092048 // 0b11 00 00 01 00 10 01 00 00 01 10 00 01 00 00
String [4] fizzbuzz= {"%u\n","Fizz\n","Buzz\n","FizzBuzz\n"}
For(i=1;i<=100;i++){
}

I like how it's both beautiful and ugly.

The mask is basically an array of what to print and because it circles around every 15, circle shifting works

It avoids the reusing of modulos/variables which is often the ugly part of fizzbuzz

But it abuses printf (using a variable as a parameter but not using it) and is generally fairly obscure if not commented.

It also needs to be reworked if you need the fizzbuzzification of an arbitrary number.

Though tbh it's fairly similar, just using a lookup table instead of calculating modulo at runtime
• 0
@satibel Also a nice one, took me a little to figure out how the 15 period goes into the 32 bit int, but you're effectively using only 30 bits for the rotating shift. The main upside is avoiding a modulo operation because modulo may be rather slow, depending on the CPU.

However, the code as is doesn't provide the correct output (change mask to 19142723 and it does work), and there should be | for the binary or, not +, although in this case, the result of the operation is the same.
• 0
@satibel 19142723 = 0b00 00 01 00 10 01 00 00 01 10 00 01 00 00 11
810092048 = 0b11 00 00 01 00 10 01 00 00 01 10 00 01 00 00
The bits count the other way around. :-)
• 1
@Fast-Nop I used an online tool converter for the binary, it probably got flipped from little endian to big endian.

The code is mostly pseudo code, I didn't check it so it's a miracle if I just had the flip problem (and it should work with the number as binary)

Tbh this version is probably decently fast on avr style hardware.

An option to avoid print formatting is to change the first value of the array by doing
itoa(i,fizzbuzz[0],10)
before printing
(and obviously allocating enough memory in fizzbuzz [0])
• 0
vh,jv,kk.vhhv
• 0
@alern87 you might have fallen asleep on your keyboard, or you're a bottom.