Algorithms Worth Knowing Cold

You can clear most interviews with a small set of algorithms drilled to muscle memory. Knowing them cold — meaning you can implement them in 10 minutes without bugs — is more valuable than knowing 30 algorithms vaguely.

The non-negotiable list

Sorting

  • Quicksort and mergesort — understand both, know their time/space tradeoffs.
  • Counting / bucket / radix — useful when input is bounded; mention them when constraints fit.

Search

  • Binary search — including the off-by-one variants for "first true" and "last false". Drill these until you can write them in your sleep.

Graph traversal

  • BFS — shortest path on unweighted graphs, level-by-level processing.
  • DFS — both recursive and iterative (using a stack). Know how to detect cycles.
  • Dijkstra — shortest path on weighted graphs (non-negative weights).

Dynamic programming

The skill that separates strong candidates from average ones. Master:

  • 1D DP — climbing stairs, house robber, max subarray.
  • 2D DP — edit distance, longest common subsequence, knapsack.
  • Tree DP — house robber III, diameter of binary tree.

The trick to DP is identifying the state: what set of values, when known, is enough to solve the next step? If you can articulate the state, you're 80% done.

Recursion & backtracking

  • N-queens, sudoku, subsets, permutations — the canonical examples.
  • The pattern: try, recurse, undo. Keep the state on the call stack rather than mutating shared structures.

Greedy

  • Interval scheduling, Huffman coding, activity selection. Greedy works when local optimum implies global optimum — and proving that is half the battle.

What you don't need to memorize

Implementation details of algorithms that have library functions in every modern language. Nobody is going to ask you to implement sort() from scratch in a real interview. Know how it works conceptually, but spend your time on patterns instead.

Communicating algorithms in interviews

Always state the algorithm, then complexity, then code. Something like:

"I'd use a sliding window with a hash map of character counts. That's O(n) time and O(k) space where k is the alphabet size. Let me code it up."

That sentence is half the interview.