FizzBuzz - my first interview task on whiteboard
I think we all know the FizzBuzz task, when we a function shall print fizz if the number is divided by 3, buzz if by 5 and fizzbuzz when by both. Yesterday I did a small research about how many different implementations we can have using C# and there were many. Ones looks very common with "if" statement tree, some of them were using LINQ and looking hard to understand what is going on in a first instance.
StackOverflow has this one question with a bunch of examples Writing FizzBuzz, that I will use to check how those solutions work from a performance perspective, code complexity and human understanding.
listing 1 fizzbuzz question
As we can see, OP introduced a tuple to solve the problem, which in my opinion gives a lot of opportunists for extensions with a little code changes.
The metric looks good, but if we move the list declaration outside the function (so it is not recreated every time the function is called we shall be good)
To do benchmark I will convert all those functions to return string for a given integer, so you can see this file https://github.com/profesor79/FizzBuzz/blob/main/01MareInfinitus.cs
As we can see, the function performs great unless we have fizz buzz case, as it needs to add those strings and that takes a lot of CPU time, and moving the declaration outside a function increased our maintainability index:
As author claiming it does the job, but it will be hard to extend in an easy way if we need to cover more cases, but the metrics (after changing to return the result as a string) are great:
Now as we are returning string value, I got rid of the "else" statement and magically we drop our index as the number of code lines increased (imho that is a shame...)
Now, as there was a difference in metrics I decided to run two tests, one with removed if/else other keeping that.
It is very hard to say what is the best option as the results are same (and we shall take at least 20 probes of same to be able to say something about true results (and as one run takes 3 minutes - I'm not going to bother with it).
the file is here: https://github.com/profesor79/FizzBuzz/blob/main/02ThomasLevesque.cs
Now, there is a solution that is saying: "Unrolled for maximum efficiency. This program can outfizzbuzz all others."
As we can see on the snippet - it is just funny to code like that, metrics going orange as we have a lot of lines of code here. Now this is the one solution that can't be tested using a single input as it runs by 15, so most common requirement to work from 1..100 is not fulfilled.
the benchmarked code is here: https://github.com/profesor79/FizzBuzz/blob/main/_03Xela.cs
Now we are going to see something that has very low metrics:
Now can we do it without IF statement, in a way just do it?
as we can see on the listing above it is a bit of preparation to get it, and this piece of code get's the best metrics (again some stuff is outside the function so it is a kind of cheating)
We did it without if with a price of CPU ticks (there probably will be a person that will do it faster), but this was just to prove the concept.
Now look at this:
I have got no questions about that!! This is insane, PatternMatching is treated as one liner, easy to follow and is that fast?
THIS IS INSANE!!!
Comments
Post a Comment