Wednesday, July 8, 2020
7 Simple Ways to Improve Code Quality In Interviews
7 Simple Ways to Improve Code Quality In Interviews 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. Its also worth to note that theres 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, well give you very detailed and practical tips to improve code quality instead of telling you some very general ideas. Some tips wont take you 30min to acquire, others may take a little longer. They are all very practical and you dont 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 shouldnt 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 its 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. Its a terrible practice to name your variable like x, y, z although it may save you some time. You dont 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, its 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, its 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. 4. Concise Try to keep you code short. There are many benefit of this. First of all, its less likely to have bugs as its said that ratio of bugs per line of codes is a constant. Secondly, itll 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 Its 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 dont 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: Its a utility function that is separate from the main logic, like Swap function. Its reused multiple times, then its a good practice to write it once. its 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 hes 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. Theyd 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 its 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. Conclusion 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 dont want to see crappy code in our mock interviews any more. Any other ways to improve code quality?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.