Everything you need to know for performing better in algorithmic interviews!
Hello people, how’s your day been till now? Are you ready for another article based on algorithmic interviews? Let’s make a move then! Our previous articles covered all the topics related to data structure and algorithm based interviews. We have discussed topics like why specifically algorithm interviews are conducted, the concerns related to it, and the possible improvements that could be made to it. After all such discussion, I believe it is now time to discuss how to prepare for algorithm interviews!
Till date, a lot of people have asked me about shortcuts to crack Google interviews. As per my experience, the only existing shortcut is to focus on your computer science foundation. One needs to have a strong computer science foundation and if it isn’t strong, work on it first! The key here is to play the long game. Practicing coding questions is a good habit and a must, but practicing without clearing the basic concepts is of no use! I would consider this to be the most common mistake which candidates make. If you want to be a good performer, you need to stay there for a longer period of time. The folks which boast about themselves by performing better in the short term are making a mistake because one day, they will have to come back to the basics. The way a building needs a strong foundation, a successful engineer needs a strong computer science foundation.
Data-structure and algorithm concepts need to be very clear. One might be able to come up with a solution, but for coming up with an optimum solution, proper understanding is a must. Even writing a bug-free code sounds impossible without having the right idea. Once you are through with the concepts or the foundation, you will save a lot of time while coding and thinking. The primary goal should be to focus on making the computer science basics foundation stronger. Then and only then, one must begin interview preparation.
When I discuss the importance of having a solid computer science foundation, a lot of people ask me what it actually means? Even while explaining, some people ask me questions like, I am able to write code and I know bubble sort, so does it mean that I am good? So let us understand it in a better way. When it comes to coding interviews, foundation refers to having a clear understanding of basic data structures and algorithms. I will explain this step by step in this article. But before that, please try and answer the following questions:
Number 1: If you are allowed to use any one of the linked list, array, stack, queue and other data structures for a list of objects, how will you decide which one to use? What will be the biggest advantage or disadvantage of using it?
Number 2: How does dynamic programming and recursion differ from each other? How would you compare a recursive solution and its iterative version?
Number 3: I want to improve my time complexity solution from n square into n, what is the possible algorithm that comes to your mind?
Does it seem like a lot of questions? Well, these are just a few sample questions that came to my mind. If you are able to answer these questions, bingo! You are good with the concepts. But if you are not able to answer them, revisiting your data structures and algorithms book would be the best idea!
It is now time to discuss the computer science foundation step-by-step. So let us decompose it!
Always remember that cramming won’t help, so understand the data structure and algorithm concepts properly rather than cramming it. I believe that some concepts are a bit confusing but understanding them is a must. For example, what is the difference between dynamic programming and recursion? Or stack versus heap, how are they used in memory? Such questions aren’t easy to tackle. Deal with them carefully. Guys, this article is not meant for giving you specific answers, but it is meant to make you understand how to prepare for the interview. You will have to find out the specific answers yourself. Google would happily help you on that.
One of the best examples to mention here would be quicksort. The algorithm isn’t easy but is helpful. How would you find the kth largest number in an array by implementing it? You will end up writing the same piece of code again. Candidates often get confused with questions like which data structure or algorithm should be used in this situation? This happens because they aren’t clear about the pros and cons of each data structure and algorithm. Fortunately, most of the resources and books group similar tools within the same chapter. For example, linked list, array, stack, and queue are generally considered under one group. While BFS and DFS are considered as a part of another group.
In such a way, you will have to do the homework for every single data structure or algorithm. You have to ask yourself that if I use this approach, will it be beneficial or not? When should I use it? I practiced in the same way and has benefitted me a lot.
I have seen that a lot of people ignore complexity. I remember that as an interviewer, with every coding question I also used to ask about time and space complexity. Not just me, most of my colleagues who have been interviewers asked this question. One should always be clear about the complexity. For every algorithmic question that you practice, never forget to find out about the complexity. If you already have a habit of finding the complexity, it is good because it has always proved to be pretty helpful. Especially when you need to find out which is the suitable algorithm for solving a problem, knowing the complexity factor would help you in deciding it. For example, if you come up with a solution during an interview and the interviewer finds your solution to be slow, he/she will ask you to optimize it.
Like if you have come up with a solution having complexity n square, we can think to improve it. How? For improving speed, we will have to choose another data structure or algorithm that is better under this scenario. Similarly, if we want to make the complexity n log n, we can use binary search, sort, BST, etc. Maybe, the array can be sorted first. This might prove to be an advantage. For memory search, hash is a good option and DP might also be a good way for optimization. So thinking in this way would always help you because you will end up with fewer options and these fewer options would be the good ones.
Many resources are available from where you can learn about these topics, like The Technical Interview Cheat Sheet, Big-O cheat sheet, etc. I also particularly like the book which is titled as Introduction to Algorithms which helps in clearing the basic concepts. Some sections might be considered as optional, but overall this book is quite good. Personally, as an interviewer, I will never pass a candidate who seems to be confused about the basics. It only means that the candidate is not sure about what he/she is doing. On the other side, if the candidate is not able to solve a problem completely, this doesn’t mean that his/her concepts aren’t clear.
Let’s summarize here. This article showcases the importance of having a strong computer science foundation which is the most important and the primary necessity of solving any problem. Not just for clearing an interview, a solid foundation is necessary for solving basic problems as well. We have first discussed what exactly does solid foundation mean? We then understand it by discussing a few questions and then switching over to a step by step analysis of building a strong computer science foundation in three steps.
I always recommend engineers to refresh their concepts timely. You don’t need to mug up, but you should refresh what you know. In fact, every time I refresh my concepts, I learn something new out of it. Go through each and every data structure and algorithm one by one. Understand the concept, try figuring out the advantages and disadvantages, pick up a random example and solve it by writing the code, and compare it with other algorithms. Only then, begin practicing the coding questions. In short, become more efficient by making your basic foundation stronger.
Here is the link to the previous article of this series.