5 proven steps to ace your coding interview

5 proven steps to ace your coding interview

Imagine you are in the technical round of your dream company and the Interviewer asks you a question which looks pretty hard and you don't know how to solve.

Here are 5 steps that can help you think about solving the challenge.

Problem Solving Strategies

  • Understanding the problem
  • Explore concrete examples
  • Break it down
  • Solve / simplify
  • Look back and refactor

Let me take through blog with a single and simple example so that we understand better. 

Example Question : write a function which takes a string and returns counts of each character in the string.

Understand the problem

     When the interviewer asks you to write a code, first make sure you understand it and ask yourself these questions 

  • Can I restate the problems in my own words ?
  • what are the inputs go into problem ?
  • what are the outputs that should come from the solution to the problem ?
  • can the outputs be determined from inputs?, In other words do I have enough information to solve the problem ?
  • How should I label the important pieces of data that are part of the problem ?

So let's try by applying these questions in the example question given above and these are my answers (you can write your own).

  • Can I restate the problems in my own words ?  - "return occurance count of each character in a given string"
  • what are the inputs go into problem ? - can it accept a string with space in between ( 'hello world') ?, can I accept character other that letter and number ?
  • what are the outputs that should come from the solution to the problem ? can i return as object with character as key and its occurance as its value, can i return the character and its occurance as array pair ?
  • can the outputs be determined from inputs?
  • How should I label the important pieces of data that are part of the problem ? - declare an object. If the letter is found for first time declare the key and value with 1 or increment if it is already found.

     These are some of the question to be asked ourselves, you can add your own questions which will help you to clear your head.

Explore Concrete Examples

     Coming up with examples can help you understand the problem better. Examples also provides sanity checks that your eventual solution works how it should. Here are the steps

  1. start with simple example
  2. progress to more complex examples
  3. explore examples with empty inputs
  4. explore examples with invalid inputs
No alt text provided for this image

Actually this step will be useful when you do your own projects to understand the edge cases. I have given some of the test cases and how our code should handle those.

Break it down

Explicitly write out the steps you need to take. the steps you write doesn't have to be valid syntax. This will be very useful for the white board or white paper interview, where the interviewer expects you to explain the basic steps that you need to take . So that he also understand your way of approaching the problem in a better manner. This would also help you as guide flags then and there to keep you in track while solving a big problem.

      This force you to think about the code you'll write, before writing it & helps you catch any lingering conceptual issues or misunderstanding before you dive in & have to worry about details as well. so I would write the basic steps of break down like this.

No alt text provided for this image

By articulating the steps like this would not only help you to follow your code. It will also make the interviewer know that you have formulated a path to the solution. so even if you did not come-up with the solution, he would consider your steps towards solution.

Solve and Simplify

     When you are confident about 80% of the problem but you feel tedious about one/two things about the probem this is where simplify comes in. So it is always better to start writing the code you know rather that thinking about solving the hardest part of your code. Here are some steps to follow to simplify the code.

  • Find the core difficulty in what you're trying to do.
  • Temporarily ignore that difficulty.
  • Write a simplified solutions.
  • Then incorporate the difficulty back in.

Now to solve the question, I would start with plain and simple approach without considering about handling space and other special characters

No alt text provided for this image

And the answer would look like this 

No alt text provided for this image

In the second execution of the program you could see that I have not handled the space and the uppercase characters. But we are very close to the solution. And if you are not sure about some existing functions (like how to lowercase the string or uppercase the string) you could even ask for small helps then and there to the interviewer and come up with a better solution. May be you could have some more solutions for the same question, you can demonstrate them about your problem solving ability.

Look back and refactor

     So we have come to the final step which is the most important step. You might have come up with a super solution, an average solution or scrappy solution, this step is to look back your code and refactor (improve your code). It is always always always important to look back at your code even if you have come up with a super solution. 

Here are some of the refactoring questions

  • can you check the result ?
  • can you achieve the result differently ?
  • can you understand it at a glance ?
  • can you use the result or methods for other problem ?
  • can you improve the performance of your solution?
  • can you think of other ways to refactor?
  • How have other people solved this problem ?

So in our question this can be improved by using regular expression, which allows us to check whether the given input is not a letter or number.

No alt text provided for this image

Hmm.. now our soultion seems better. But it is not over yet. Still we can optimize it.

No alt text provided for this image

I have used the modern "for of" loop rather than the traditional one. So I hope you might now know the importance of refactoring the code.

So we have come-up with a solution at last with some simple techniques. I hope these steps would help you to solve the coding challenge given to you.

I am going to post a blog daily, don't forget to follow me. 😋

To view or add a comment, sign in

More articles by Deepak Paramesh

Others also viewed

Explore content categories