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.