Interview Questions As C++ One-Liners

There can be many advantages to knowing your main language really well. One of these is that when interviewing you will be much more comfortable coding without an IDE or a language reference. Another is that many languages will have some nice features built in which can be really helpful in the types of questions you typically find in a technical interviews. One great example of this can be found in Python, where reversing a string is actually less characters than the word “reverse” as seen here:

My main language is C++, and while it doesn’t tend to have things quite so cute, it does have some great opportunity for nice 1-liners in competitive programming and technical interview style questions. For example, to reverse a string:

Not as adorable as the python version, but still pretty clean. Let’s look at some other questions!

Given two equal sized arrays of integers, return the number of times that a[i] > b[i] for each index i.

With this question we are able to leverage std::inner_product as a generalized form of our problem by providing our own definition of product – std::greater. This will add up the sum of results of greater (which is 1 for true or 0 for false) and return the results, solving our problem.

Given a container of integers, compute the sum of all the elements in the container.

std::accumulate does exactly what we want here, all we have to do is specify the initial value of 0.

Given two ints n and m, round n up to the nearest even multiple of m.

This one is a bit trickier as the problem has two parts – positive and negative numbers. Handling those cases in a ternary helps us keep our solution concise. If performance is an issue, you could compute the modulus operation only once and store it for reuse.

Given a string, determine if it is a palindrome.

This solution solves the problem in place by taking advantage of reverse iterators – it will iterate over the length of the string from the front and back, comparing them as it goes. By using begin + size/2 instead of end, the algorithm will stop halfway through the string, which is all that is necessary in a palindrome.

The point here is that if you are very comfortable in a particular language and familiar with the abstractions it offers, you can take problems which might appear to be somewhat difficult and solve them with a trivial amount of effort.

Design Patterns: Builder

Imagine you are working on a project and need some sort of GUI for it. There are lots of different parts which work together to make a GUI, and you have to have all of them just right to really get your application looking sharp. In some languages, it can be quite easy to do this, for example let’s say you want to make a button. Let’s pretend this button needs to ┬áhave a height, width, position, anchor point, color, and text. Sometimes the defaults are what you want, whereas other times you want to specify only certain values. In languages like C# which allow named arguments, that’s really easy and clean. For example:

This syntax is great. In addition to it being very clear to the reader what each argument is going to be used for, it is also going to work correctly if someone down the line changes the order of the parameters for the Box constructor. Unfortunately this feature is not available in many of the commonly used programming languages. If you wanted to do something similar in C++ you might end up with code that looks similar to this:

This does the job, but for the reader it isn’t immediately obvious what each of those arguments will be used for. It also is susceptible to silent bugs like switching the order of height and width in the constructor – it wouldn’t cause an error but would result in your boxes being sized incorrectly.

Fortunately there is an elegant solution – the builder pattern! To implement the builder pattern all you have to do is implement a single method for each property you wish to have support building. If you already have setters for these properties it becomes even easier, as all you need to do is change the signature and body to return a reference to the class instance. Here’s a C++ example of this:

If you have one of these for each of the properties you wish to have support building, your client code can easily chain them together like this:

While it’s not as pretty as the C# version, it’s still a very clean solution which allows the client code to customize only what it wants, without having to type a bunch of boilerplate code.

A Case For FizzBuzz

FizzBuzz is a classic interview question that is known to many programmers. If you’re not familiar with it, it goes something like this:
Implement a function that prints all of the numbers from 1 to 100. For numbers divisible by 3 instead print “fizz”, for those divisible by 5 print “buzz” and for those divisible by both print “fizzbuzz”. Usually each of these are space or newline separated. The output should look similar┬áto this: Continue reading “A Case For FizzBuzz”

Return Value Optimization Behavior

Recently I had an interview and the topic of performance came up – I had a reasonably large piece of data (around ~1KB) which I created in a function and returned by value in my solution to the interviewer’s question. The interviewer asked “if you are returning by value, why don’t you std::move instead?” and I replied with a comment about Return Value Optimization (RVO). Afterwards, I have considered this response, and it led me to question what the actual behavior of the more popular compilers is when it comes to different situations in which one might expect RVO to apply. This is because according to section 12.8/31 of the C++ standard, an implementation may perform RVO even if the construction or destruction of additional instances has side effects, but it is not a requirement.
Continue reading “Return Value Optimization Behavior”