Photo by Jexo on Unsplash

How to Answer a Coding Interview Question?

A step-by-step guide to help you answer a coding question.

--

Coding interviews are an essential part of various software-related positions, such as software engineer, machine learning engineer, data scientist, etc. Most companies use coding for their technical screening. This means that if you are not able to code well, they will not be moving forward with your interview process. Earlier this year, when I was applying for various ML engineer positions, nearly 85% of my initial technical screening interviews consisted of coding rounds.

Using coding as a part of your daily work does not guarantee that you will perform well in these coding interviews. I learned it the hard way, after failing a couple of coding interviews. The ability to code in a certain language is just the tip of the iceberg when it comes to interviews ([F5] in the section below). Time management, relating to similar questions, polishing your basics, and practicing are some of the key things that need to be done. In this article, I will share my organized approach to preparing for a coding interview. I will divide this article into two parts.

What are you being evaluated on?

The first thing to understand is what are the interviewers interested in so that we can address them accordingly. The interviewer usually evaluates you on the following evaluation criteria (EC)

[EC1] — Ability to understand the question

[EC2] — Ability to gather information when needed

[EC3] — Your thought process when presented with a problem

[EC4] — Ability to code in an organized/modular manner

[EC5] — Your coding skills and ability to assess the run-time constraints

[EC6] — Ability to realize improvements in your work

The Coding Template

I found it really useful to define a template and use that during my coding interviews. The template helps compartmentalize your thought process in an organized fashion to avoid talking too much about one thing and leaving no time for others. It also helps with targeting at the necessary factors [EC1-EC6] that the interviewer is looking for.

How to answer a coding question? — The template (Image By Author)

1. Listen to the question:

The interviewer will explain the question and go through a toy example to help you understand the problem. While the interviewer explains it, note down the important points on a separate sheet of paper, that you think are key takeaways.

Target: [EC1]

2. Talk about your understanding of the question:

After listening to the question, DO NOT jump in and start coding (even if you know exactly what the problem is and how to solve it). Instead, repeat the question and confirm your understanding. Ask clarifying questions such as

  1. What are the Input/Output data type limitations?
  2. Is there any restriction on the Input size/length?
  3. What happens if the input is invalid?
  4. Corner/special cases: Problem-specific questions such as what happens when you see a non-digit character in a string where the two digit-strings needed to be multiplied? (Do you consider it as invalid input, or do you ignore the non-digit character?)

Most of the time the interviewer does not give you all the required information. One of the things that the interviewer is looking for in a candidate is being able to ask the right questions to gather all the necessary information

Target: [EC1], [EC2]

3. Discuss your approach:

Discuss your approach to the problem and ask the interviewer if he/she agrees with it. Talk about the data structure you prefer to use and the reason behind it. Discuss the pseudo-code with the interviewer.

Target: [EC3]

4. Start coding:

Always ask the interviewer before you actually start coding. Define useful functions and explain as you write the code. The most important thing while you code is to think out loud so the interviewer can evaluate your thought process. With each line of the code, say out loud why you are using it and how this choice will impact the code output. For example, while writing a for loop, say

“Now we will define a for loop to iterate over the xyz list one element at a time so that we can process each element according to the desired output [or problem-specific reason]”

In case you are stuck somewhere, the interviewer will drop in subtle hints. Make sure you pay attention to those hints.

Target: [EC4], [EC5]

5. Discuss the time and space complexity

Discuss the time and space complexity of your code in terms of Big O for your approach. This useful resource can be a good starting point for beginners. Try to break down your code into chunks, discuss the time complexity, and then talk about the overall time complexity of the code.

Target: [EC5]

6. Optimize the approach (if possible or the interviewer suggests)

After discussing the complexity of your code, the interviewer can ask you to improve it, if your approach is not already optimized. The interviewer will drop by hints highlighting the chunk of code that can be improved. Make sure you pay attention to that.

Target: [EC5], [EC6]

This 6-step template will help you divide and conquer the interview question.

Summary:

Coding interviews are not just about your coding skills. It's also about how well you can work under pressure. If you have a well-understood template to follow, it will definitely help manage your time. Knowing what the interviewers are looking for will help you address those factors in an organized manner. This article looked at some of the important evaluation criteria for a successful coding interview and talked about a template to answer the coding question.

You may also find the following article useful

If this article was helpful to you or you want to learn more about Machine Learning and Data Science, follow me Aqeel Anwar, or connect with me on LinkedIn or Twitter.

--

--