Data Structures Cheat Sheet

For coding interviews or computer science classes

A quick reference of the big O costs and core properties of every data structure.

Time Complexity of Common Operations

index search insert delete
Array
index
search
insert
delete
An array of letters from a to f with numbered indices 0 through 5.

Stores things in order. Has quick lookups by index.

Full array reference

Dynamic Array
index
search
insert
delete
The dynamic array has a size of 3 and a capacity of 6.

An array that automatically grows as you add more items.

Full dynamic array reference

Linked List
index
search
insert
delete
A linked list with the letters a,b,c,d. The letter a points to b, b points to c and c points to d.

Also stores things in order. Faster insertions and deletions than arrays, but slower lookups (you have to "walk down" the whole list).

Full linked list reference

Queue
index
search
insert
delete
A queue composed of the letters a, b and c. c is the first (rightmost) item, then b and then a.

Like the line outside a busy restaurant. "First come, first served."

Full queue reference

Stack
index
search
insert
delete
A stack composed of the letters a, b and c. a is the first item and placed at the top of the stack, followed by b and then c.

Like a stack of dirty plates in the sink. The first one you take off the top is the last one you put down.

Full stack reference

Hash Table
index
—
search
insert
delete
An array that contains 4 keys and corresponding values: key "apple" has value 4, "cherry" has value 2, "pecan" has value 5 and "blueberry" has value 1.

Like an array, except instead of indices you can set arbitrary keys for each value.

Full hash table reference

Binary Search Tree
index
search
insert
delete
A binary search tree with nodes containing integers. The root node contains the integer 50. Each child node to the left of the root contains integers less than 50, and each child node to the right of the root contains integers greater than 50.

Everything in the left subtree is smaller than the current node, everything in the right subtree is larger. <span complexity="lgn"></span> lookups, but only if the tree is balanced!

Full binary search tree reference

Trie
index
—
search
insert
delete
The trie has four branches and represents four different words: "POT", "PAST", "PASS", "PART". All words are sharing parent node "P". Words "PAST", "PASS", "PART" are sharing node "A". Words "PAST and "PASS" also sharing node "S".

Stores a set of strings in a big tree of characters. Good for lookups by prefix. <em>Sometimes</em> saves space.

Full trie reference

Heap
index
search
insert
delete
A binary heap is a binary tree where the nodes are organized to so that the smallest value is always at the top.

A binary tree where the smallest value is always at the top. Use it to implement a priority queue.

Full heap reference

Priority Queue
index
search
insert
delete
An element with high priority which is equal 1 is served before elements with lower priority: 2, 28, 39, 84 and 99. So the new element with priority 2 will be served before an element with priority 28 but after the existing element with priority 2.

A queue where items are ordered by priority.

Full priority queue reference

Bloom filter
index
—
search
insert
delete
—
hash("lemon") % 8 = 2. The bit at index 2 is O, so "lemon" is DEFINITELY NOT present in the set (true negative).

A constant-space bitmap that lets you quickly check whether or not an item is in a set. Can give false positives.

Full bloom filter reference

LRU Cache
index
—
search
insert
delete
Doubly-linked list with a dictionary where each item contains links to both its predecessor and its successor.

Lets you quickly identify which item hasn't been used for the longest amount of time.

Full lru cache reference

Get the 7-day crash course!

In this free email course, I'll teach you the right way of thinking for breaking down tricky algorithmic coding questions.

No CS degree necessary.

. . .