Skip to content

Complexity Cheatsheet

Big O time & space complexity for all data structures covered in this tutorial.


Linear Structures

StructureAccessSearchInsertDeleteSpace
ArrayO(1)O(n)O(n)O(n)O(n)
StackO(n)¹O(n)O(1)²O(1)²O(n)
QueueO(n)¹O(n)O(1)²O(1)²O(n)
Priority QueueO(n)¹O(n)O(n)O(1)²O(n)
DequeO(n)¹O(n)O(1)²³O(1)²³O(n)
Circular QueueO(1)⁴O(n)O(1)O(1)O(n)

¹ Access by value (no index). Stack: peek is O(1). Queue: front/rear is O(1). ² At the ends only. Stack: push/pop. Queue: enqueue/dequeue. ³ Deque: both ends. ⁴ By index within the array.


Linked Structures

StructureAccessSearchInsertDeleteSpace
Singly Linked ListO(n)O(n)O(1)¹O(n)²O(n)
Doubly Linked ListO(n)O(n)O(1)¹O(1)²O(n)
Skip ListO(log n)³O(log n)³O(log n)³O(log n)³O(n log n)

¹ Insert at head is O(1). Insert at tail requires traversal for singly linked lists. ² Delete given a node reference: Doubly is O(1), Singly is O(n) (need previous node). ³ Average case. Worst case is O(n).


Hash & Set

StructureSearchInsertDeleteSpaceNotes
Dictionary (Map)O(1)⁴O(1)⁴O(1)⁴O(n)
SetO(1)⁴O(1)⁴O(1)⁴O(n)
Hash TableO(1)⁴O(1)⁴O(1)⁴O(n)
Bloom FilterO(k)⁵O(k)O(m)No delete, false positives

⁴ Average case. Worst case O(n) with many collisions. ⁵ k = number of hash functions.


Trees

StructureSearchInsertDeleteSpace
BST (balanced)O(log n)O(log n)O(log n)O(n)
BST (skewed)O(n)O(n)O(n)O(n)
AVL TreeO(log n)O(log n)O(log n)O(n)
Heap (Min/Max)O(n)¹O(log n)O(log n)²O(n)
Trie (Prefix Tree)O(k)³O(k)³O(k)³O(n × k)

¹ Search in a heap is O(n) — heaps are not designed for general search. peek() is O(1). ² Extract min/max is O(log n). General delete requires search. ³ k = key length.


Graph

RepresentationEdge CheckIterate NeighborsAdd VertexAdd EdgeSpace
Adjacency MatrixO(1)O(V)O(V²)O(1)O(V²)
Adjacency ListO(degree)O(degree)O(1)O(1)O(V + E)

Graph Traversal

AlgorithmTimeSpaceUse Case
BFSO(V + E)O(V)Shortest path in unweighted graphs
DFSO(V + E)O(V)Path existence, topological sort

Caching

StructureGetPutEvictionSpace
LRU CacheO(1)O(1)O(1)O(capacity)

Specialized

StructureUnionFindConnectedSpace
Union-FindO(α(n))O(α(n))O(α(n))O(n)

α(n) = Inverse Ackermann function — effectively O(1) for any practical input size.


** Quick Tips**

  • O(1) → Constant time. Best possible. Hash lookups, array indexing.
  • O(log n) → Very fast. Binary search, tree operations.
  • O(n) → Linear. Single pass through data.
  • O(n log n) → Good sorting algorithms (Merge Sort, Quick Sort).
  • O(n²) → Quadratic. Nested loops. Avoid for large datasets.

MIT Licensed | Made with ❤️ for JS learners