Basic Algorithms That You Need to Know by Heart to Crack Your Interviews

Daiki Nakashita
Analytics Vidhya
Published in
5 min readFeb 16, 2022
Photo by Clément Hélardot on Unsplash

I learned to code on my own after college and have been working as a software engineer for the past 6 years. I mentioned my background and how I got into this field to my friends, and a lot of them asked me what they should study and whether they need to go to a BootCamp to become a software engineer. The simple answer is no. You don’t need to go to school to become a software engineer. You can set foot in the software industry without a formal education in computer science. You just need to work on coding projects and try making Android applications or something. (You’ll get paid significantly less than your CS major counterparts for the initial 2–3 years, though.) So, What are the differences between software engineers that received a formal education in computer science and the ones that taught themselves?

For one, the ones that received a formal education have been exposed to more aspects of computer science while you may have only worked on Android/IOS applications as a self-taught programmer. But the biggest difference that affects your hirability and your practical software engineering skills is probably the understanding of algorithms and data structures. You won’t know the difference between lists and dictionaries, the performance of those data structures, or even how and when to use them.

So here are some of the fundamental algorithms that you absolutely need to know by heart to pass the future coding interviews.

  1. Valid Parentheses

This problem and the solution give you an idea of how to use a stack and a dictionary.

Dictionaries are used to store data in key/value pairs. It has the average (amortized) time complexity of O(1) and the worst time complexity of O(n) for lookups, meaning that if you wanted to check if a value is present in a dictionary with a particular key, your computer can do it really fast most of the time, but if you're extremely unlucky, it might take a long time.

There’s also a very similar data structure called a set. Sets are primarily used to store multiple values in order to keep track of whether a particular data value has already been added.

Before I explain the other data structure mentioned above called a stack. Here’s a good article to read to understand what FIFO and LIFO are.

  • For a FIFO (First in First out Queue), the element that goes first will be the first to come out.
  • For a LIFO (Last in First out Queue), the element that is entered last will be the first to come out.

(The LIFO queue data structure is also commonly called a stack since the visual representation of its behavior is similar to a stack of pancakes where what’s placed last will be the first one to be taken out.)
You need to use these data structures to efficiently keep track of and process data in your algorithms.

2. Time to Sell Stock

Sometimes you are given a problem and solve it, and the interviewer tells you to optimize it or says something like “you can do it in one pass”. They would probably tell you that if you didn't know this optimized solution (unless you are smart enough to come up with it on the spot.)

3. Tree Traversals

You should do some research on tree structures and in-order, pre-order, post-order traversal algorithms if you’re not already familiar with them, but the key takeaway from these solutions above is that you can always turn a recursive function into an iterative function, and a lot of times, the iterative solution will be more memory-efficient.

4. Binary Search

This is a very widely known algorithm, you should definitely review this before your interview (along with all the other ones on this article….)

5. Depth-first search

I had to work on this problem and the similar variations of it during my past interviews so many times, and I already memorized it by heart, and you probably should too.

6. Fibonacci Sequence

They will always have you optimize your naive recursive solution with a dictionary…

You are wondering why you need to use a dictionary and retain the previously calculated values?
It’s super nicely explained in this Youtube video.

The last one with the yield keyword is only a must for python developers.

Bonus: Effective Python and Cracking the Coding Interview

If you’re a python developer, I highly recommend checking this book out.

Also, every software engineer probably needs this book as well.

(I don’t work for Amazon or anything …. I wish though.)

Of course, there are a lot more things you need to know to pass the interviews and work productively and efficiently. You need to learn about everything from sorting algorithms to new frameworks and what microservices are and what could potentially be the bottleneck in your application.
But in terms of the interview algorithm questions, some companies seem to give their candidates incredibly tough problems, and when you encounter one of those companies… maybe they aren’t trying to hire you.
(Also some companies would give you super lengthy coding assignments for you to complete within a week or so. You‘ve just got to say no to those)

--

--

Daiki Nakashita
Analytics Vidhya

UC San Diego grad, working as a software developer in a foreign land while struggling to get a grasp of the culture and language.