Top 7 Coding Interview Algorithms


Top 7 Coding Interview Algorithms

Corey
Alright, let's talk about the seven most important algorithms you need to know for coding interviews. We've got three search algorithms—binary search, depth-first search, breadth-first search—then three sorting algorithms—insertion sort, merge sort, quick sort—and finally, greedy algorithms. We'll cover what each is, when to use it, and how efficient it is.

Michael
That's a solid lineup. But before we dive in, remember you need to be comfortable with basic data structures and time complexity. If those are fuzzy, it's worth brushing up first.

Corey
Absolutely. Let's start with binary search. It finds an element in a sorted list by repeatedly dividing the search range in half. Think of the guessing game where you pick a number from 1 to 100 and your friend says higher or lower. Instead of starting at 1 and going up—that's linear search, O(n)—you start at 50, then cut the range in half each time. That's O(log n), way faster for big lists.

Michael
And the key is the list has to be sorted. If it is, binary search is usually the first thing to try. It's one of those algorithms you've probably used in real life without realizing it, like looking up a name in a phone book.

Corey
Exactly. Now, the next two—DFS and BFS—are for searching trees and graphs. They come up all the time in interviews, right after arrays in frequency. Let's start with depth-first search, or DFS. The idea is to start at the root and go as far down one branch as possible, all the way to the end, then backtrack to the nearest unvisited branch. You keep a visited array to avoid revisiting nodes.

Michael
So it's like exploring a maze: you follow a path until you hit a dead end, then backtrack to the last junction and try another path. DFS was actually invented for mazes. And its runtime is O(V + E), where V is vertices and E is edges.

Corey
Right. On the other hand, breadth-first search, or BFS, works level by level. You start at the root, visit all its neighbors, then move to the next level. You use a queue to keep track of neighbors. BFS also runs in O(V + E) time. A classic example is in chess algorithms, where you explore all possible moves for the next turn before looking deeper.

Michael
So BFS is great for finding the shortest path in an unweighted graph, while DFS is often used for topological sorting or detecting cycles. Both are essential for tree and graph problems.

Corey
Now let's move to sorting. Insertion sort is the simplest: it builds the sorted list one element at a time by inserting each new element into its correct position. It's O(n) in the best case when the list is already sorted, but O(n²) in the worst case. So it's best for small or nearly sorted lists.

Michael
That's a good starting point, but for larger lists, we need something faster. Merge sort is a divide-and-conquer algorithm that splits the list in half recursively until you have single elements, then merges them back in sorted order. It runs in O(n log n) in all cases, which is consistent.

Corey
And merge sort uses O(n) extra space because of the merging. Quick sort is also divide-and-conquer, but it picks a pivot, partitions the list so that elements less than the pivot come before it and greater after, then recursively sorts the partitions. Its best case is O(n log n), but worst case is O(n²) if the pivot is poorly chosen. However, with good pivot selection, it's often 2-3 times faster than merge sort on average, and it uses O(log n) space because of recursion.

Michael
So why would anyone use quick sort if it can be O(n²)? Well, in practice, the worst case is very unlikely with random data or good pivot selection, and the constant factors are better. But merge sort is more predictable, so it depends on the situation.

Corey
Exactly. Now for something different: greedy algorithms. A greedy algorithm makes the best choice at each step without looking ahead, aiming for a local optimum in hopes of a global optimum. They don't always give the best result, but they're useful when an optimal solution is impossible to compute.

Michael
Can you give an example where greedy fails? Suppose you have a graph where each edge has a cost and you want the cheapest path from start to end. A greedy algorithm might pick the cheapest edge at each step and end up paying 16, while the actual optimal path only costs 3. So it's not efficient, but sometimes we don't have a choice.

Corey
Right. The classic example is the traveling salesman problem: visiting all cities with the shortest possible route. As cities increase, the number of possible routes explodes factorially—for 50 US state capitals, the number of routes is astronomically large. A greedy algorithm, like always going to the nearest unvisited city, gives a reasonable approximation in a fraction of the time.

Michael
So greedy algorithms are for problems where finding the perfect solution is impractical. That wraps up the seven: binary search, DFS, BFS, insertion sort, merge sort, quick sort, and greedy algorithms. Each has its place in an interview, so knowing them well is a huge advantage. Thanks for breaking them down.

Corey
Glad to help. Remember, practice implementing them in code, and you'll be set for interview questions that involve searching and sorting.

Ask anything
We've read this whole study set, so questions about it get the best answers.