Advanced Data Structures Cheat Sheet

Here are the heap, deque, tree, and graph operations you'll actually write in an interview, and how fast each one runs.

What's on the sheet

  • Heap. You want the smallest (or largest) item ready in O(1), and you don't need the rest sorted.
  • Deque. You need a queue or a stack, and a deque gives you O(1) at both ends with one class to remember.
  • Tree. You're walking parent and child links top-down, one level or one branch at a time.
  • Graph. You've got nodes and edges you look up by key, so build an adjacency list first.

Watch out

  • Heap: Tuples compare left to right, so two equal priorities fall through to the second value. If that value can't be compared (like a dict), Python throws a TypeError. Push a counter in the middle, (priority, count, item), so the tie never reaches it.
  • Deque: A list only tracks one end efficiently. Popping from the front slides every remaining item over, which costs O(n). A deque tracks both ends directly, so popleft() finishes in O(1) no matter how long the queue is.
  • Tree: BFS holds a whole level in its queue at once, which is O(w) space for the widest level. DFS only holds the current branch, which is O(h) for the height. A wide, shallow tree favors DFS, and a deep, narrow one favors BFS.
  • Graph: A tree only has one path in, but a graph can loop back on itself. Skip the visited set and a cycle spins the traversal forever. Mark a node visited the moment you enqueue or recurse into it, not when you finally process it.

How to get it

This one is free and you don't need an account. Download the PDF above whenever you want it.

The ten pattern sheets work differently. You earn each one by solving half of that pattern's problems. See the pattern sheets.

Every pattern has a sheet too. See the full set.