As a very first thing, I must say that I don’t like whiteboard coding exercises during the job interviews.
These exercises have almost never any relation to real-life programming problems (“Reverse a linked list?” If your reply is anything besides “linkedList.Reverse();”, your answer is wrong.). There are only a few dozen common whiteboard coding questions – and every candidate with a common sense will review those before the interview.
Finally, success with whiteboard coding seems to have next to no connection with the actual qualities of the candidate. And yes, this includes “solving a problem under pressure”.
I much prefer to give the candidates a small task, to do at home in 2..3h. This will give me much more information – what is his coding style? Can he write high-quality and well-commented code? Did he actually manage to finish all parts of the task – and if not, then why? Does he even want the position enough to do a proper job?
That all said, FizzBuzz does have one good use – it can be used to quickly weed out the candidates who cannot program at all. If they cannot do a simple logical exercise, and write a loop with a couple of if-clauses inside it, they really shouldn’t be hired as developers.
That is why ex-coworker Erti-Chris and I were rather surprised after reading “Tales of a Non-Unicorn: A Story About The Trouble with Job Titles and Descriptions” (reddit thread). A person claiming to be “designer/developer if there ever was one” goes “OMG MATH” and cannot solve FizzBuzz (which has almost nothing to do with math)?!
Out of boredom we started to play around with FizzBuzz – albeit in C#, not JS (but all our ideas work with Javascript as well, with a little prodding) – first the “standard solution” and then limiting ourselves further and further.
The “standard solution” using modulus:
- Enumerable.Range(1, 100).ToList().ForEach(x =>
- {
- var d3 = x % 3;
- var d5 = x % 5;
- Print(d3 == 0, d5 == 0, x);
- });
Method Print is used repeatedly, it is the same everywhere:
- private static void Print(bool three, bool five, int i)
- {
- if (three && !five)
- Console.WriteLine("Fizz");
- else if (five && !three)
- Console.WriteLine("Buzz");
- else if (five)
- Console.WriteLine("FizzBuzz");
- else
- Console.WriteLine(i);
- }
But what if we don’t allow modulus to be used?
- Enumerable.Range(1, 100).ToList().ForEach(x =>
- {
- var d3 = x / 3f == Math.Round(x / 3f);
- var d5 = x / 5f == Math.Round(x / 5f);
- Print(d3, d5, x);
- });
But what about not using neither round nor modulus?
- Enumerable.Range(1, 100).ToList().ForEach(x =>
- {
- var d3 = x / 3 * 3 == x;
- var d5 = x / 5 * 5 == x;
- Print(d3, d5, x);
- });
(and also the same limitations as above, nutcase version. Proudly mine).
- Enumerable.Range(1, 100).ToList().ForEach(x =>
- {
- var d3 = !(x / 3f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
- var d5 = !(x / 5f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
- Print(d3, d5, x);
- });
Still too easy. What about not allowing modulus, rounding, multiplication and division?
- for (int i = 0, three = 0, five = 0; i < 100; i++, three++, five++)
- {
- if (three == 3 && five == 5)
- {
- Console.WriteLine("FizzBuzz");
- three = 0;
- five = 0;
- }
- else if (three == 3)
- {
- Console.WriteLine("Fizz");
- three = 0;
- }
- else if (five == 5)
- {
- Console.WriteLine("Buzz");
- five = 0;
- }
- else
- Console.WriteLine(i);
- }
Amusingly, the last version is fastest of them all.
And by now we were out of time and bored of this. It was fun, though – a sort of minor coding kata.
All the code above can be seen and executed in dotNetFiddle, https://dotnetfiddle.net/84PodI
Lisa kommentaar