Top 7 Coding Interview Algorithms
7 Essential Algorithms for Coding Interviews
Search Algorithms
Binary Search
Binary search is a search algorithm used to find the position of a specific element in a sorted list. It works by repeatedly dividing the search interval in half.
How it works: Start with the middle element. If the target is higher, eliminate the lower half and repeat on the upper half; if lower, eliminate the upper half. Continue until the target is found or the interval is empty.
Visual example: Guessing a number between 1 and 100. Instead of starting at 1 (linear search), start at 50. With each guess, eliminate half the remaining numbers. The worst‑case number of guesses for 1–100 is about log2(100)≈7 guesses, versus 100 for linear search.
Time complexity: O(log n) – logarithmic. For a list of 10,000 elements, binary search takes at most 14 guesses in the worst case.
Important constraint: The list must be sorted for binary search to work. If you see a sorted list and need to find an element, binary search is often the best starting point.
Depth-First Search (DFS)
Depth-first search is an algorithm for traversing or searching tree and graph structures. It explores as far down a branch as possible before backtracking.
How it works: Start at the root node. Go down one branch until reaching a leaf (a node with no unvisited children). Then backtrack to the last node that has an unvisited branch and explore that branch. Use a visited array to avoid revisiting nodes.
Real‑life example: Solving a maze – explore each path to its end; if it’s a dead end, backtrack to the nearest fork and try another path.
Time complexity: O(V + E), where V is the number of vertices (nodes) and E is the number of edges (connections).
When to use: When you need to explore all possibilities along one path before trying others, or when memory is a concern (DFS uses less memory than BFS on wide graphs).
Breadth-First Search (BFS)
Breadth-first search traverses a tree or graph level by level. It explores all nodes at the current depth before moving to the next depth.
How it works: Start at the root node. Add it to a visited array. Add all its neighbors to a queue. Dequeue the first neighbor, mark it visited, and add its unvisited neighbors to the queue. Continue until the queue is empty.
Key difference from DFS: BFS processes nodes in order of increasing distance from the root; it does not go deep before exploring all nodes at the current level.
Real‑life example: Chess algorithms – evaluate all possible moves one turn ahead before considering moves two turns ahead. (Note: the video incorrectly calls this DFS; BFS is the level‑by‑level approach used in many game‑tree searches.)
Time complexity: O(V + E) – same as DFS.
When to use: When you need the shortest path in an unweighted graph, or when you want to visit nodes in order of increasing distance from the start.
Sorting Algorithms
Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final sorted list one element at a time by repeatedly inserting the next element into its correct position among the previously sorted elements.
How it works: Start with the second element. Compare it to the element before it; if it is smaller, swap them. Continue comparing with the previous element until it is in the correct position. Move to the next element and repeat.
Time complexity:
Best case: O(n) – when the list is already sorted (only one comparison per element).
Worst case: O(n²) – when the list is reverse‑sorted (each element must be compared with all previous ones).
When to use: Best for small lists or lists that are already mostly sorted. As the list grows larger and more unordered, the O(n2) runtime becomes problematic.
Space complexity: O(1) – in‑place sorting.
Merge Sort
Merge sort is a divide‑and‑conquer algorithm that recursively splits the list into halves, sorts each half, and then merges the sorted halves back together.
How it works:
Divide the list into two halves repeatedly until each sublist contains only one element.
Recursively merge pairs of sublists, comparing the first elements of each and placing the smaller one into the new merged list.
Continue merging until the full list is reassembled in sorted order.
Time complexity: O(n log n) in the best, average, and worst cases – it always divides and merges regardless of the input order.
When to use: Best for large, unsorted lists where consistent performance is needed. More predictable than quick sort.
Space complexity: O(n) – requires additional memory for temporary sublists during merging.
Quick Sort
Quick sort is another divide‑and‑conquer algorithm that works by selecting a pivot element and partitioning the list into two sublists: elements less than the pivot and elements greater than the pivot. It then recursively sorts the sublists.
How it works:
Choose a pivot (ideally the median of the list).
Move the pivot to the end of the list.
Place a left pointer at the start and a right pointer just before the pivot.
Move the left pointer right until it finds an element greater than the pivot. Move the right pointer left until it finds an element less than the pivot. Swap those two elements.
Continue until the pointers cross, then swap the pivot back into its correct position (the left pointer).
Recursively apply the same process to the left and right sublists.
Time complexity:
Best / average case: O(n log n) – when the pivot divides the list roughly in half each time.
Worst case: O(n²) – when the pivot is the smallest or largest element each time (e.g., already sorted list with a naïve pivot selection).
Why use it despite O(n2) worst case? When implemented correctly with a good pivot strategy (e.g., random pivot or median‑of‑three), quick sort is on average 2–3× faster than merge sort because its inner loop can be highly optimized and it uses less memory.
Space complexity: O(log n) – due to recursive stack space (in‑place partitioning requires no extra list).
Drawback: More difficult to implement correctly; poor pivot selection can degrade performance.
Comparison of sorting algorithms:
Insertion sort: O(n) best, O(n2) worst, O(1) space – good for small/nearly sorted data.
Merge sort: O(nlogn) all cases, O(n) space – stable and predictable, good for large data.
Quick sort: O(nlogn) average, O(n2) worst, O(logn) space – fastest on average, but requires careful implementation.
Greedy Algorithms
A greedy algorithm makes the locally optimal choice at each decision point, hoping that this will lead to a globally optimal solution. It never looks ahead to consider future consequences.
When not to use greedy algorithms: They do not guarantee the optimal solution for all problems. Consider a path‑finding problem where each edge has a cost. A greedy algorithm might always pick the cheapest next edge (e.g., 7 instead of 8), leading to a total cost of 16, while the true optimal path costs only 3. For small problems, exhaustive search (DFS or BFS) can find the exact optimum.
When to use greedy algorithms: When the problem is so large that it is impossible to compute the exact optimal solution in a reasonable time. Greedy algorithms produce a good approximation that is often close to optimal.
The Traveling Salesman Problem (TSP):
Problem: Given a list of cities and distances between each pair, find the shortest possible route that visits every city exactly once and returns to the starting city.
Number of possible routes: For n cities, the number is (n−1)!/2.
5 cities → 12 routes (easily solvable)
10 cities → 181,440 routes (solvable in milliseconds)
50 cities (e.g., all US state capitals) → a number so large it is written as:
3041409320171337804361260816606476884437764156896051200000000001000 cities → a number with thousands of digits, far beyond the capability of any computer to enumerate.
Greedy approach for TSP: Start at an arbitrary city. At each step, travel to the nearest unvisited city. After visiting all cities, return to the start. This produces a route that is not necessarily optimal, but much better than random selection and computable in polynomial time.
Key takeaway: Greedy algorithms are used when finding the exact optimal solution is computationally infeasible. They sacrifice perfection for efficiency and provide a reasonably good answer in a short time.

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