Algorithms and coding Cheat sheet

Algorithms and coding

Tick a problem or variation once you can solve it without the hint. Ticks stay in this browser; nothing is sent anywhere.

How to code in an interview

  1. Restate the problem in one sentence, and ask about input size, empty input, duplicates and negative numbers.
  2. Say the brute force and its cost before anything clever. It's a correct answer you can improve.
  3. Name the pattern ("this is a sliding window") and the better complexity.
  4. Write it while talking. Clear names over short ones. Syntax matters less than structure.
  5. Run one small example by hand, then the edge cases: empty, one element, all the same.
  6. State time and space at the end, and one way you'd improve it if they asked.

Complexity cheat sheet

OperationCost
dict / set lookup, insertO(1) on average
list append, pop from the endO(1)
list insert or pop at the frontO(n): use collections.deque for O(1)
sortO(n log n)
heapq push / popO(log n); heapify O(n)
binary searchO(log n) on sorted data
BFS / DFS on a graphO(V + E)
n up to 10^5 or 10^6aim for O(n) or O(n log n)
n up to about 20O(2^n) backtracking is fine