7 Steps to Acing a Coding Interview
- Clarify the problem
- Generate inputs and/or outputs
- Create test cases
- Plan your program
- Analyse the performance: speed/runtime
Step 1: Clarify the problem to be solved. This is important to make sure that you do not spend wasted effort on solving something that the interviewer is not even interested in due to your misunderstanding. It is often helpful to even just repeat your understanding of the problem to make sure that you and the interviewers are on the same page.
Step 2 and 3: Brainstorm some expected inputs and outputs that your solution would face. These also include some tests and edge cases (e.g. empty arrays, negative numbers, null, etc.) that the program may get as inputs. Then decide how your solution would handle that.
Step 4: Develop some possible solutions and pick the best one. Based on your inputs and outputs, you may have a few ideas, such as treating your problem as a graph, then using Breadth First Search for finding the minimum distance between two points in the graph, or perhaps you have a more naive approach. Pick one solution to use, preferably the one you’re comfortable with that can be justified.
Step 5: Code. Usually, you would have to write a function that takes in some inputs. You want to first catch and handle the “edge cases” such as empty inputs. The main part of the code would usually contain for loops combined with some conditional statements in a typical coding interview. Make sure to make the appropriate variable initializations. Consider writing some helper functions too to do certain things that can be abstracted out from the main logic.
Step 6: Once you have a solution, make a short analysis of the runtime (in Big O terms) and if your solution is not optimal, maybe try to propose alternative solutions that can have better performance. You do not have to re-do your solution, but just make it known to the interviewer that you’re aware of such potential runtime issues.
Step 7: Even if you’re coding on the whiteboard, you may score bonus points with the interviewer if you try to debug your program. Besides checking for syntax errors, take a few example inputs and step through your code using these sample inputs to check if your code would work as you intended it to be. This would also help you to spot mistakes that you may have overlooked when coding the solution the first time.
Finally, don’t forget to practice. There are lots of websites and communities on the web that provide ample practice problems, such as Interview Cake, Project Euler, HackerRank, Topcoder, LeetCode, The Coding Interview, and more.