A step-by-step procedure for solving a problem or accomplishing a task.
A simple program on your PC can take either seconds or hours to run, and it all depends on the algorithm used. An algorithm is an exact sequence of steps the program takes to reach an answer. It's a procedure that a computer follows very precisely, so you need to be careful to choose a good algorithm. Optimizing an algorithm is one of the easiest ways to unlock a performance upgrade, and it's a skill you can master over time. Good luck!
These are not all the algorithms in the world. There are thousands, if not millions, of famous algorithms developed by programmers around the world to solve a specific issue (check out Chess Programming for some wacky algorithms!). However, the algorithms below will equip you with a methodical way of thinking that will allow you to not only understand existing algorithms but also invent and optimize your own. This stuff will genuinely change the circuitry in your brain (check out this research paper).
These are the foundational algorithms that everything else builds on. I urge you to spend extra time within these pages to try and understand when and why to use a certain algorithm. You will notice that algorithms are often closely tied to a certain data structure, which is why I have also linked algorithms at the bottom of every data structure's page.
Checks every pair without visiting every pair. Two markers closing in from opposite ends replace an entire nested loop.
Detect an infinite loop without marking a single node. If the fast runner ever laps the slow one, the list has a cycle.
When one element enters and one leaves, the rest of the work is reusable. That single trick turns quadratic scans linear.
Find any entry among a billion sorted items in about 30 looks. Each guess throws away half of what remains.
Pay for one pass up front and the sum of any range becomes a single subtraction, no matter how often you ask.
A function that calls itself sounds like it should run forever. Add a base case and it becomes the cleanest way to shrink a problem down to nothing.
Every node visited exactly once, none missed, none repeated. The order you choose decides what the walk computes.
The setup move for everything else: once data is sorted, searching, merging, and finding duplicates all get cheap.
These algorithms are harder because instead of doing a single pass on the input, we are now actively making decisions on the data that we see. The majority of your time will be spent here since this type of problem-solving takes a while to grasp, but the visuals will get you through, I promise.
Try a choice, follow it until it dead-ends, undo it, try the next. Brute force with an undo button, and it solves Sudoku.
When each answer builds on the previous few, store them once and stop recomputing. Exponential problems collapse to a single pass.
Grab the best option available and never reconsider. For the right problems, that shortcut is provably optimal.
Meetings, bookings, IP ranges. Sort by start time and most overlap problems untangle in a single sweep.
Thirty-two yes/no answers packed into one integer, each readable in a single operation. This is the machine's native language.
Commit to one path until it bottoms out, then back up and try the next. The recursion stack remembers the way home.
Explores in expanding rings, so the first time it touches a node is also the shortest way there. No weights needed.
Frankly, this stuff is hard to whip out mid-interview if you've never seen this before. The other algorithms actually give you a fighting chance of working through it, but there's a reason these algorithms are named after people. Some of the people who invented these algorithms went on to win the Turing Award, the highest achievement in computer science, for inventing them (I'm talking about Dijkstra). Don't be frustrated, just give yourself time to understand why and how they work.
When a problem has two moving parts, the memo grows into a grid. Edit distance and longest common subsequence live here.
Track the running median of a stream without ever sorting it. Two heaps lean against each other and the answer sits where they meet.
The idea behind your GPS: always extend the cheapest path found so far, and the first arrival is guaranteed optimal.
Dijkstra with a sense of direction. A hint about where the goal lies lets it skip most of the map and still return the best path.
Every shortest path between every pair of nodes, computed by three nested loops you can write from memory.
Shortest paths even when some edges have negative cost, plus a built-in alarm for cycles that make the question unanswerable.
Wires every node together for the least total cost by growing one network outward, always taking the cheapest edge.
Same goal as Prim, opposite move: sort every edge, then keep adding the cheapest one that doesn't close a cycle.
Orders tasks so every prerequisite comes first, or proves that no valid order exists. Build systems run on this.
With no cycles to fear, shortest paths stop needing Dijkstra. One topological pass and every distance falls out in linear time.
Algorithms optimize the efficiency of programs, allowing tasks to be completed quickly and using fewer resources. The gap between O(n) and O(n²) isn't noticeable when your input is 10, but at 10,000,000 the difference is too obvious.
An algorithm's complexity measures how much time and space it needs as its input size (often called n) grows. It's usually written in Big-O notation, which is the upper bound of an algorithm's time or space requirements in the worst-case scenario.
Time and space costs are a tradeoff that you need to make when picking algorithms. Faster algorithms usually use more memory, and memory-efficient algorithms tend to take more time.
Here is how I approach problem-solving when working with something I've never seen before. I have a quick checklist I run through as soon as I'm done reading a problem: