How do you write perfect code in an interview?
From industry experience and Gainlo interviewers feedback, majority of candidates wrote crappy code in interviews, which could be bugs, terrible coding style, inconcise, confusing and so on. It’s also worth to note that there’s a significantly better chance for cleaner code to get hired because they tend to have less bugs, easy to understand and look much more professional.
Like other posts in this blog, we’ll give you very detailed and practical tips to improve code quality instead of telling you some very general ideas. Some tips won’t take you 30min to acquire, others may take a little longer. They are all very practical and you don’t need to have years experience or write a lot of codes.
1. Language selection
Most companies allow candidate to choose whatever languages they like and the most common ones are definitely C++ and Java. Although they shouldn’t have a big difference as both languages are so popular, we see significant advantages when using C++ compared to Java.
First of all, C++ has a much more concise syntax. Remember that normally you only have no more than 20min to solve a coding question and if removing the time you spend on thinking and discussing, you would usually have less than 10min to code your solution. With that in mind, C++ would allow you to write the exactly same solution with less code and less time. Try to compare list/vector and class definition, you will definitely get an idea how verbose Java is.
Second, C++ pointer is a great tool in code interview. Like many people, I personally hate pointers as they are prone to error and ugly. However they are quite useful in a code interview due to its flexibility. For instance, it allows you define a method that can return multiple values by passing pointers as params.
Besides C++ and Java, Python would be a better choice although not everyone is familiar with it. Its concise syntax makes it so powerful in interviews and if it’s an option for you, just use it.
2. Good coding style
Although every company may have different coding styles, you should still pay attention to it since bad coding style makes you look not professional. You can follow Google style guide for Java, C++ and Python, which are very popular.
Also you should care about your variable naming. It’s a terrible practice to name your variable like x, y, z although it may save you some time. You don’t need to make it super long and the point is to make it easy to understand and debug for both you and the interviewer.
3. Keep your code complete and clean
When I say complete, it means that your code should consist of everything it needs to compile and run. If the question is asking you to implement a function, please start with the declaration of it instead of writing the inside directly. If you are asked to define class, it’s essential to start with class definition.
Also you should declare all your variables. Surprisingly, we saw a lot of people were using variables from nowhere, which really scared me.
To make your code clean, I mean its appearance. When you write your code on a white board, it’s always good to make your writing clean. Some letters might be confusing in writing like “q” and 9 or “z” and 2, please make sure that your handwriting is easy to understand.
Also please avoid using arrows to insert your code as much as you can, which is just ugly. You can put extra spaces between each line in case of inserting codes or just be slower and more careful when coding.
Try to keep you code short. There are many benefit of this. First of all, it’s less likely to have bugs as it’s said that ratio of bugs per line of codes is a constant. Secondly, it’ll impress your interviewer when your solution is correct and short. Lastly, it just saves you some time by writing less characters.
Of course this requires a lot of practice and the number one suggestion is when practicing, always refine your solution to make it shorter and try to recognize certain patterns that you use to do that. You should feel uncomfortable for duplicate codes or some unnecessary logic.
Besides, some simple hacks can also make it easier for you.
- Use C++ auto in loops. Instead of writing
for(int i = 0; i < array.size(); ++i)
you can use auto in the loop like
for(const auto& element : array)
- Put “if” and “for” in a single line when possible. For example:
for(const auto& ele : array) if (ele.attr) res[++i] = ele.val;
- Use Python list comprehension.
5. Always validate input first
It’s a very simple practice, however most people forgot to do that. Check if the input is NULL, empty, positive etc. and never assume you are given the valid params. This is a good practice for both interview and your production code.
Also when you try to test your solution, try with some weird input and see what will happen. Make sure your solution is robust enough to handle all those corner cases.
6. Avoid defining unnecessary functions
A common pattern we see from many candidates is they tend to define many unnecessary functions, which is more often when they get stuck. Not only will these functions waste your time, but it makes the code hard to read.
Most interview questions don’t need to define more than 2 functions normally and you should be careful if your new function is not in one of the following cases:
- It’s a utility function that is separate from the main logic, like Swap function.
- It’s reused multiple times, then it’s a good practice to write it once.
- it’s a different stage/phase of the main function, like pre-processing or post-processing.
7. Talk while writing
Many candidate keeps silent while writing code, which is not recommended although we normally write code without talking to anyone. The idea here is that talking gives you a chance to communicate with the interviewer, which has a lot of benefit.
First, it makes it so much easier for the interviewer to understand your code. Most people assume that their codes are easy to read, however they are only easy to themselves. As a result, interviewers have to ask candidates to explain their codes in the end and try to understand them, which may take a lot of time. A smart candidate will explain what he’s doing while writing the code and probably the interview got everything right after he finished.
Second, it allows the interviewer to correct you or give you hint when you are not on the right track. Believe it or not, most interviewers want you to succeed. They’d like to give you hint when you get stuck. By communicating with them while writing code, you actually give them the chance to better understand what you are doing and correct you immediately when it’s wrong. On the contrary, silent coder may end up with a complete but incorrect solution after 20min and he got no time to improve it.
Third, it also gives yourself a chance to be aware of what you are doing. People tend to keep struggling when they are in trouble and most of the time they are doing nothing except wasting time. By explaining your code, you are more likely to know whether you are really solving the problem or just doodling.
Remember that in most companies, interviewer will copy your code on white board (maybe by taking a phone or just writing down) and submit to the hire committee. So your code plays a very important role when making a hire decision. Try to refine your code when practicing and find someone to criticize it.
I hope you get a few big takeaways from this post and I don’t want to see crappy code in our mock interviews any more.
Any other ways to improve code quality?