The insider's guide to algorithm interview questions

Algorithms are a big part of coding interviews, especially at the big 5 tech companies (Google, Microsoft, Facebook, Apple, Amazon). We’ll take a look at some common algorithms that you'll need to know for an upcoming interview, ways to make them more efficient (as that is a common question asked in interviews), and at the end we'll give you some practice problems.

Once you have the basics down, you can start to move into intermediate concepts that you should know before scheduling your technical interview; specifically, this post will show you some key algorithmic paradigms (and ways to make them efficient) which are fundamental to learning how to break down any coding problem in an interview. The following information assumes that you’ve brushed up on data structures. If you haven’t, check out these refresher courses on data structures.

Asymptotic Analysis — boosting the efficiency of your program

Before diving into algorithmic paradigms, something should be said for the time complexity and efficiency of computer programs in relation to algorithms — a concept known as asymptotic analysis. In an interview, you may not be asked to calculate the complexity of an algorithm directly, but you might be asked to calculate the complexity of an algorithm you wrote or to improve upon the complexity of an algorithm given to you.

Complexity is an approximate measure of the efficiency of an algorithm and is associated with every algorithm you write. This is something that all developers must be constantly aware of. There are two kinds of complexities: time and space. Time complexity and space complexity are essentially approximations of how much time and how much space an algorithm will take to process certain inputs respectively. Typically, there are three tiers to solve for:

Best case — represented as Big Omega or Ω(n)

Average case — represented as Big Theta or Θ(n)

Worst case — represented as Big O Notation or O(n)

Big O is preferred to analyze an algorithm, as average and best cases do not give insight to the efficiency of an algorithm for most use-cases.

Every time a list or array gets iterated over c x length times, it is most likely in O(n) time.

When you see a problem where the number of elements in the problem space gets halved each time, it will most probably be in O(logn) runtime.

Whenever you have a singly nested loop, the problem is most likely in quadratic time.

Useful Formulae for calculating time complexity of an algorithm:

What are algorithmic paradigms?

Algorithmic paradigms are “general approaches to the construction of efficient solutions to problems”; in other words they are a method, strategy, or technique to solving a problem and are essential for every programmer. Spend time learning these, as you will most likely use one or two of these algorithms in an interview.

Algorithmic paradigms are great because they lay down a framework suited for solving a broad range of diverse problems. Examples include:

Divide and Conquer — a pattern that breaks the problem into smaller subproblems which are then solved for recursively and combined (great for tree sorting).

Backtracking — an algorithmic-technique for solving problems by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem.

Dynamic Programming — an optimization algorithm for recursive functions. Using dynamic programming, you can store the results of subproblems, so that we do not have to re-compute them when needed later.

What algorithms should I be aware of for my interview?

Brute Force — This method requires us to go through all the possibilities to find a solution to the problem we are meaning to solve. This is often the algorithm that comes to mind first, and even though it may be the least efficient, you’re guaranteed a solution.

Greedy Algorithms — an algorithmic paradigm that builds up a solution piece by piece, meaning it chooses the next piece that offers the most obvious and immediate benefit.

Graph algorithms:
Implement a function that returns the number of nodes at a given level of an undirected graph.

Greedy algorithms:
Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), implement a function to calculate the number of COINS to represent Vcents.

Dynamic programming algorithms:
A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a function to count the number of possible ways that the child can run up the stairs.

Divide and Conquer algorithms:
Given a 2D array of k rows and 44 sorted columns and an empty 1D output array of size k∗ n, copy all the elements from k sorted arrays to the k∗ n output array using a divide and conquer approach.

Wrap up

If you’re heading into a technical interview you must be ready to showcase your knowledge of the different algorithms at your disposal and understand the complexities associated with each. Become familiar with the algorithmic paradigms mentioned above (i.e. divide and conquer, brute force, greedy) and as the old adage goes, “practice makes perfect”, so be sure that you put in the time to practice implementing different algorithms and calculating their complexity as it’s something that developers must be aware of when they write code.

Here are a few steps you can take to ensure success at your next interview:

Familiarize yourself with different algorithms: Don’t just memorize solutions. Take the time to understand the patterns that underlie each algorithm and the approach you should take.

Do your homework: The more you practice the more comfortable you’ll feel.

Next steps.. Study, prepare, and practice: Simply browsing through old interview questions and blogs to prep for an interview isn’t enough, you need real hands on experience. Check out this course, “Algorithms in C++: An interview refresher”, for an in-depth look at the topics covered in this post, coupled with real-world challenges and quizzes to test your understanding.