Technical problem-solving approach
You might come across interviews where you could not crack a coding question or sometimes you might be preparing for a technical coding interview. It is very important that you must communicate with the interviewer at every step in the process of solving a problem. He/She might give you some clue when you try to solve a problem which will greatly help you. In this article, I will get you through the ways to solve a technical coding problem. I have chopped down approach to problem-solving to several steps which are explained below.
1. Listen/Read
If you have given a technical question to solve from home, read the question thoroughly to extract all the information from the question. If you understand the question thoroughly, you can come up with the right algorithm to solve the problem. If your interviewer is shooting a question to solve it on a whiteboard, first listen to the question thoroughly. Ask for more information in case you did not understand the question properly. You can even come up with an example of how your solution should look like which will help you to get a clear picture. It will give you some time to think and also to foresee any edge cases that might occur.
2. Brute Force
After knowing the problem, make sure you come up with a solution or an algorithm to solve a problem. The algorithm that you have come up with at first does not need to be an efficient one. Your goal is just to find a brute-force method to solve a problem. ‘Something is better than nothing’. Although you cannot come up with an optimal solution if you have a solution to a problem it will score you good points in the interview. Please write the algorithm on the whiteboard or to a piece of paper. It will help you to debug any errors easily and to help you to think in a broader spectrum. Try to brainstorm if this algorithm works correctly or not.
3. Optimize
Now that you have a brute-force solution, try to find some ways to optimize it by asking a few questions. You can ask if you have used all the information that you have been given in the question. It is more usual that the solution will use all the information that has been given in the question.
- Check for the unused information. Eg. the given array is sorted, the given matrix is a square matrix.
- Make note of the time and space complexities and check if you can optimize it by reducing the time and space complexities.
- Check if you can alter the data structure so that the algorithm can be faster.
- Check if you can use a hash table to solve the problem in a better way.
4. Walkthrough
At this moment, you have an optimized algorithm with you and it is the time to explain it to the interviewer. If you are more clear at this stage, it will help you to go through other steps in an easy manner. You will avoid major errors that might commit in the coding. Explain it line by line and make sure you and your interview understand how the solution is being derived. You can also think of the code that you can use to implement the algorithm in your mind.
5. Implement
Now that you are clear with your algorithm, it is time to put your algorithm into action. You can go on and code your beautiful algorithm into the code. Make sure you maintain a clean code and coding standards that some interviewers might keep a close watch on. Make it modular and do not let anything that you do not need in the code. Assign good variable names rather than giving single-letter variables which are more meaningful.
6. Test
Testing is the final step of the coding process. You make sure to check if the concept of your code is right, then move on to test with some test cases and then to the edge cases that might occur. You can test in this order:
- Do conceptual testing which means go through your code thoroughly and check if it will solve the problem as intended.
- Check whether the loop starts with the proper value and exits the loops the right time. Prevent any deadlocks.
- Check for the code which is not standard and remove it.
- Test the code with a small test case which will save time and make it easier to solve the problem manually.
- Check for the special cases(null values, extreme values, multiple and single elements) and edge cases.
Fix all the issues that you have come up with and you are ready to go. I would like to say you that this is one approach to solve a problem. You can try several other approaches to solve a problem and choose the one best suits you. Happy coding!