The Real Answer to the FizzBuzz Interview Question

Coding Challenge Coding Live

Certainly, a short piece of code can be written using up-to-date style and best practices as applicable for software development at the scale that is involved in real projects at work.

I had a sheet with this written down, so I would always say it in the same carefully-crafted way. I lost that some years ago, but here is the problem from memory:

A popular interview question is codingFizzBuzz. Now Ive had similar experiences to what is shown atCoding Horror I had a description for a trivial piece of code that would offer insight into the depth of engineering skill, with my careful posing of the requirements.

constexpris the newstatic const. Naturally, this tabular data will be declared asconstexpr.

Code always gets more complex over time. It is an art forged by long experience to balance future maintenance with extra work now. Dont program in features that are not needed rather, anticipate what will be changed or added later and consider that in the overall design, and allow for such things when it does not add complexity to the task at hand. (I can probably write a whole essay on this topic. Message me if you have examples or general thoughts.)

voidfizzbuzz_switch(unsignedinti ) unsignedinttest =0; test = ( i%3 ==0) ?2:0;//set 3 if is fizztest = ( i%5 ==0) ?4:0;//set 5 if is buzzswitch( test &6) case6: std::cout FizzBuzz\n;break;case2: std::cout Fizz\n;break;case4: std::cout Buzz\n;break;default: std::cout No Fizz or Buzz\n;

AngularJS Interview Questions and Answers

Trivial enough? Not even a problem with logic to be considered; just a routine microtask that anyone with any fluency can express without having to think about it.

Write a class whose constructor takes two integral arguments, and has members that return their sum and their difference.

When exploring esoteric solutions I like things like this. Partially because of the misdirection, also because it demonstrates knowledge of a few things.

What I expected was to see how well the applicant processed the statement of requirements: making assumptions, or asking for clarification. I figure an experienced engineer would ask what was meant byintegral, or use atypedefto abstract the decision of which integer type to use and specify it at only one point. More fluent coders would write it as a template.

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

It is good tounderstandthe constructs used and choose them purposefully, not apply things in a cargo-cult or voodoo programming situation. On either end of the interview process, understand that simple coding problems can give far more insight than simply finding out whether the applicant can write code at all.

I was very successful in the point 2 makes the code simpler and shorter. Point 3 takes the form of putting the configuration at the very top of the file, and showing how it can be extended; and the extensibility isfreedue to the architecture noted in point 2.

The usual worries about primitive C-stylestrings do not apply since this is constant data. After all, do you worry about writingcouthello;and demand that you store that in astringobject before passing it to the function?

I was very surprised to find that most applicants who got past the screening process thus far had trouble writing a C++ class at all! The best insight I had was from someone who explained that he usually uses the IDE and doesnt know the details of syntax on how to write a class definition. Because of the reliance on what Visual Studio callswizards, we dubbed these peopleWizards Apprenticeswith a reference to thesegment from Disneys Fantasia. The story is simple: the young apprentice tries to use his masters power but cannot control it; what we would recognize as a buggy program.

Most solutions to simple problems like this mix console I/O with the logic, which is completely unlike real programs in multiple ways. It should not directly solicit input from the user as if he was a text file, or print output in the middle of the work; the solution should feature a function that takes parameters and produces results to return.

Likewise for the use of the plain array, rather thanstd::array. Arrays are not as poor as they used to be: with the preference for free functionsbegin,end,size, etc. what does the object wrapper do that the plain doesnt? Only the regular value semantics of passing and assigning and Im not doing that.

Here is the meat: a simple stateless function that accepts the number as a parameter and produces the results to return as astring.

98.4% of statistics are made up on the spot.

When I wrote this, Visual Studios C++17 compiler is still in per-release, and part of the exercise for me was to use new features and see where style needs to be updated.

I see many solutions to FizzBuzz, as well as many other problems, that are written in a way that is completely unlike what we see in real code. Someone who can effortlessly produce this might be in scripting mode which is used for single-use small programs written quickly, sloppy, and highly specialized. It doesnotillustrate good software engineering practices, and the interviewer cant tell if the applicantalways(only) writes like that, or is capable of working on projects like they have: large bodies of code, need for maintainability, testability; follows good practices, etc.

One thing that bugs me about the typical implementation of FizzBuzz is the explicit repetition of code, checking each factor and often the combination as well. Dont duplicate code that varies only by a data value! Instead, store the values and loop over them.

As with real projects, this can be called by a unit-testing main program as well as find a home in the application that needs that feature.

So, I thought about FizzBuzz as if it were a small utility feature that was ordered for use in a large project.

The loop has a one-line body, and it is automatically iterated over the data table shown earlier. That is why adding another itemjust works. This code is smaller and simpler than the cascade of tests you normally see.

please excuse the std::cout rather than returning a string.

This article, along with any associated source code and files, is licensed underThe Code Project Open License (CPOL)

But I also thought about how much good engineering I can illustrate without making the result not-so-trivial. Here is what I came up with:

Now, why did I useconst char*rather than an object type? This was a deliberate choice. First of all, the use of this table (described later) is flexible in what it can take, so I dont require astd::stringhere even though thats what will be used in the constructed object. There is no need to store astd::stringobject here, which would make another copy of the lexicalstringliterals, andstringdoes not have aconstexprconstructor.

Now suppose that youarefluent in C++ (or whichever language you are using) and would have no trouble jotting down a piece of code that does what you want.

Not only does this show fluency in C++ with up-to-date skills, it is well crafted and provides a number of discussion points. As summarized along with the code samples, I can explainwhyI did things a certain way, alternatives considered, what I might do under different circumstances, etc.

A hot-off-the-press alternative would bestd::string_view. But there is simply no need here. I chose not to usegsl::zstring, since it would be the only use of the Guidelines Support Library in the program, and there is no question that C-strings are being used since they are right there, and only right there. This is not a function parameter that needs documenting.

There are the neededincludes at the top of the file, and then the interesting part starts off with:

In themain, I avoided a legacyfor-loop by usingBoost.Range 2.0. That is tried and true for production work. Ive not tried to get the latest Visual Studio compiler drop to swallowRange-v3yet.

You mustSign Into use this message board.

Latest JavaScript Interview Questions and Answers PDF

C++ Object Relational Mapping (ORM)- Eating the Bun – Part 1 of N

Leave a Reply