Skip to content

Data Structures

First PublishedByAtif Alam

These are the main data structures that show up in the algorithms we cover. Some have their own topic page; others are the input or the building block for techniques.

Data structures here include both the forms your input might take (array, graph, tree) and the structures we use while solving (stacks, heaps, hash maps, etc.).

For a list of typical input shapes only, see Typical input types on the overview.

  • Array / list — Contiguous, indexable sequence. Used in: two pointers, sliding window, binary search, prefix sum, sorting.
  • Hash map / hash set — O(1) lookup/insert (amortized). Used in: sliding window (counts), subarray-sum problems, dynamic programming (memoization), duplicate checks.
  • Stack — LIFO. Used in: iterative DFS, backtracking, parsing.
  • Queue — FIFO. Used in: BFS, level-order traversal. In Python: deque from collections.
  • Deque — Double-ended queue. Used in: BFS, some sliding-window problems.
  • Graph — Vertices and edges; given as edge list, adjacency list (e.g. dict of neighbors), or adjacency matrix. Used in: BFS, DFS, Union Find, shortest paths.
  • Tree — Often represented like a graph (pointers or parent array) or in array form (e.g. heap). Used in: DFS, BFS, divide and conquer, heaps.
  • Heap — Priority queue; min or max at the root. Used for “kth largest,” top K, merge k lists.
  • Trie — Prefix tree for strings; each path spells a prefix. Used for autocomplete, word search, prefix matching.
  • Union Find — Disjoint set union; merge sets and ask “same set?” Used for connected components, Kruskal’s MST, dynamic connectivity.

Below are simple ASCII diagrams for how each structure is laid out or traversed.

Index: 0 1 2 3 4
+-----+-----+-----+-----+-----+
Array = | 10 | 20 | 30 | | |
+-----+-----+-----+-----+-----+

Contiguous memory layout:

[ A ][ B ][ C ][ D ][ E ]
Head
|
v
+------+ +------+ +------+ +------+
| 10 | -> | 20 | -> | 30 | -> | 40 | -> NULL
+------+ +------+ +------+ +------+

Node structure conceptually:

+-----------+
| data | next ---->
+-----------+

Key → value mapping:

"apple" -> 5
"banana" -> 3
"grape" -> 8

Internal bucket representation (with hashing):

Index
0 -> NULL
1 -> ("banana", 3)
2 -> NULL
3 -> ("apple", 5) -> ("grape", 8)
4 -> NULL

Conceptually:

Key ----hash----> Bucket ----> Value
+------+
Top -> | 30 |
+------+
| 20 |
+------+
| 10 |
+------+

After push(40):

+------+
Top -> | 40 |
+------+
| 30 |
+------+
| 20 |
+------+
| 10 |
+------+
Front Rear
| |
v v
+------+ +------+ +------+ +------+
| 10 | -> | 20 | -> | 30 | -> | 40 |
+------+ +------+ +------+ +------+

Dequeue removes from front.

10
/ \
5 15
/ \ \
2 7 20

General tree structure:

Root
/ | \
N1 N2 N3
/ \ \
N4 N5 N6

Undirected graph:

(A) ----- (B)
| \ |
| \ |
(C) ---- (D)

Directed graph:

(A) --> (B)
^ |
| v
(D) <-- (C)

Adjacency list representation:

A: B, C
B: D
C: D
D: A

For which Python type to use (list, dict, set, deque, heapq), see Python Data structures.