Recently I have started giving lots of coding interviews online. Lately, when I was asked to solve a coding problem, I realized that my approach to solving the problem was not good, which always made me move in the wrong direction and not solve the problem. I started studying my way of solving a problem and realized what I was doing wrong.
After lots of study and efforts, I came up with Eight (8) steps to solve a coding problem. After following these steps, I solved the problem with ease and in a proper way. Here I'm sharing with you the tips to solve a coding problem. Let's go ahead with an example question.
Let's assume you were asked this question.
n positive integers
a1, a2, ..., an, where each represents a point at coordinate (i, ai)
vertical lines are drawn such that the two endpoints of the linei
is at(i, ai)
and(i, 0)`. Find two lines, which, together with the x-axis, forms a container, such that the container contains the most water.
height = [1,8,6,2,5,4,8,3,7], the function should return
This is a crucial step your interviewer is expecting you to do, which is to verify the constraints.
Now, what are constraints? Constraints are ways we can understand the question better so that, you know, what kind of edge cases you need to consider, as well as all of the different variables that might impact how you solve the question.
And this usually comes in the form of a dialogue, which means that you want to ask your interviewer specific questions to answer and figure out what these constraints are.
So let's walk through some great questions for this exact question.
- The first question that we want to ask is, are all of the numbers positive or can there be negatives? This is a great question to ask when working with integers because depending on whether or not the integers are positive or negative. This changes the approach and the solution that we come up with.
- The next question we might ask is, are there duplicate numbers in the array? Meaning are all of the numbers inside the array receive unique, or can there be duplicates?
- Another good question that we want to ask is, will there always be a solution available? So this is a great question to ask in any question case when we're doing our interviews.
- A good follow up to this will be what do we return if there is no solution?
- So another question we can ask is, can multiple pairs add to the target value.
So now that we understand all of these different constraints that we verified through these questions, we want to combine them all into different test cases.
Test cases are just different combinations of the inputs that we could receive. So different combinations of the array and the target that we could get will best capture all of these other edge cases that our solution wants to account for.
This is a process that you can work with your interviewer on where you ask them. Is it OK if we come up with some test cases together that best capture these different questions that you've helped me answer for our constraints?
Once you asked all those different questions related to the constraints. Now is the best time to come up with some of the test cases that will capture all of the various constraints.
Remember, there can be many different constraints that we need to make sure we capture in multiple different test cases.
This is a very crucial step that we have to make because what we're doing here is we are thinking about coming up with a working solution before we get bogged down by any technical details. Here, I want you to think of a logical working solution for how you might solve this question if you were just asked this question out of the blue.
So if there's no code involved, you don't want to think about for loops while loops, variables, any of that stuff, all you want to think about is if I were to approach this problem critically and logically, how would I ensure that I have a working solution that works for all of the test cases that we just came up within Step 2.
Don't think about optimal solutions yet because a working solution is better than a non-working optimal solution.
Once you know how to approach the problem and have a good, logical working solution, you want to move on to the next step, which is to write out this solution you came up with within code.
In the last step, you came up with a logical solution to this problem, and in this step, you're going to convert that logical solution into a code solution.
You now have the brute force solution you came up with without any optimizations, but it's a working solution. So once you fully coded the solution you came out with, you want to take this time to move on to the next step.
So here, what you need to do is you need to make sure that you didn't make any spelling mistakes, we didn't make any typos. Our variable names are consistent wherever we use them. Also, make sure that all of our cases are correct. You also want to make sure that we're closing all of our loops and closing all of our functions.
The primary key here is just that you want to do the due diligence check to ensure that your code is exactly how it should be for it to work correctly.
So, at this point, this is often where many people get tripped up because they're not used to writing code by hand.
Usually, we rely on our I.D.E as a crutch. And this is why this practice is good because by practising and doing this double-check for errors, you'll often find that you catch yourself for minor mistakes that you would have made if you were using an I.D.E as well. But the difference is that the ** IDE** catches it for you. Your interviewer is looking for these mistakes, and they're trying to see if you are a careful and cautious coder. So it would be best if you double-checked for errors. Once you see that your code is all correct, then we move on to our next step.
At this step, what we're doing is we are testing the solution we wrote against our test cases. This is a very crucial step because your interviewer is going to ask you to do precisely this. They want to make sure that you understand what every step of the code you wrote is doing. And the only way that they can verify that is by asking you to walk through your solution using one of the test cases. So they want you to keep track of all the variables you're setting and how the code will flow, and your logic. So this is very important for you to practice because if you're not practised with it, you will get stumbled upon having to walk through this code so rigorously.
So the more you practice doing this, the better because you're going to eat up a lot of your interview by doing this step. But it's a very crucial step for your interviewer to understand that you know how your code works and that you're not just memorizing solutions.
In this step, we will discuss on analyzing the space and time complexity of the solution you came up with.
When you're analyzing space and time, complexity, you're trying to identify the relative relationship of how much of the time and space resources your code will consume based on the size of your inputs, meaning that if you have inputs that scale, which they generally always will. How much more time and space resources is your code going to take?
After calculating your time and space complexities, the question you want to ask is, can I optimize my solution?
Once you figured out if you could optimize the solution, what you want to do in this step is figure out how you can optimize the code. And one good hint that you can have is when you look at your two complexities. If one of them is drastically better than the other, then there is a good hint that you could try to bring them to the same level.
All of the techniques that we've learned here are going to be expanded upon. The primary key is how you tackle these questions and the process of working through the questions through the steps that we've laid out. That's very important because that shows your due diligence as a developer, which is a very crucial thing that they are testing you for in your interview, and make sure that throughout this process, you are also very clearly communicating just as I am when we're coding out our solution thinking through the problems. It takes a lot of practice.
These process and these steps that we're taking will also help accelerate you to help you land that dream job.
If you learned something from this article, please share it with your friends.
💌 If you’d like to receive more coding tips and tricks in your inbox, you can sign up for the newsletter here.