Advanced Data Structures Cheat Sheet

The heap, deque, tree, and graph operations you actually write in an interview, with the time complexity that explains why.

What's on the sheet

  • Heap. The smallest (or largest) item, ready in O(1), whenever you do not need the rest sorted.
  • Deque. A queue or a stack, whichever end you need O(1) at, without two different classes to remember.
  • Tree. Parent and child links you walk top-down, one level or one branch at a time.
  • Graph. Nodes and edges you look up by key, not by index, an adjacency list first.

Watch out

  • Heap: Tuples compare left to right, so two equal priorities fall through to comparing the second value, and a TypeError follows if that value cannot be ordered, like a dict. Push a counter in the middle, (priority, count, item), so the tie never reaches it.
  • Deque: A list only tracks one end efficiently, so popping from the front means sliding every remaining item over, an O(n) cost. 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, O(w) space for the widest level. DFS only holds the current branch, O(h) space for the height. A wide, shallow tree favors DFS; a deep, narrow one favors BFS.
  • Graph: A tree only has one path in; a graph can loop back on itself. Skip the visited set and a cycle spins the traversal forever, so mark a node visited the moment you enqueue or recurse into it, not once you get around to processing it.

How to get it

Free. No account needed. Download the PDF above whenever you want it.

The ten pattern sheets work differently: solve half of a pattern's problems and you earn that sheet for free, or get every one right away as a member. See the pattern sheets.

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