Phase 1 · FoundationsModule 5~36 min read

The Data Structures You'll Rely On

A fast, practical tour of the structures algorithms are built on — arrays, stacks, queues, heaps, hash maps, trees, and graphs — and their costs.

What you'll learn

Algorithms don't float in a vacuum — they run on data structures. This module is a fast, practical tour of the handful you'll use constantly, and what each operation costs. It's a reference, not a deep dive; for that, our Data Structures course builds every one of these from scratch with its own animations.

By the end you'll be able to:

  • Name the core structures and what each is good at
  • Reach for the right built-in in your language
  • Recall the Big-O of the operations that matter

The toolbox

Six structures cover the vast majority of algorithm needs:

The structures algorithms lean on

Array

O(1) access by index; the default container.

Stack / Queue

LIFO / FIFO order for DFS, BFS, and undo.

Heap / Priority queue

Always hand you the min or max fast.

Hash map / set

O(1) average lookup, insert, and membership.

Balanced BST

Sorted order with O(log n) operations.

Graph

Model relationships as vertices and edges.

Using the built-ins

You rarely implement these by hand — every language ships them. The three you'll reach for most are the stack (LIFO), the hash map, and the hash set:

Language
builtins.py
stack = []                # a list is a stack
stack.append(20)          # push
top = stack.pop()         # pop -> 20

counts = {}               # hash map
counts["a"] = counts.get("a", 0) + 1

seen = set()              # hash set
seen.add(42)
print(42 in seen)         # O(1) average -> True

Tip

A queue is just as easy: Python's collections.deque, Java's ArrayDeque, C++'s std::queue. For a priority queue reach for heapq, PriorityQueue, or std::priority_queue.

The cost cheat sheet

Keep this table close — most algorithm design is choosing the structure with the right costs for the job:

StructureAccessSearchInsertDelete
Array (by index)O(1)O(n)O(n)O(n)
Dynamic array (end)O(1)O(n)O(1)*O(1)
Stack / Queue——O(1)O(1)
Hash map / set—O(1)*O(1)*O(1)*
Balanced BSTO(log n)O(log n)O(log n)O(log n)
Binary heapO(1) peek—O(log n)O(log n)

* amortized or average case.

Choosing one

A quick mental flowchart covers most decisions:

  • Need fast lookup by key? → hash map / set.
  • Need sorted order or range queries? → balanced BST.
  • Need the smallest / largest repeatedly? → heap.
  • Need LIFO / FIFO processing order? → stack / queue.
  • Modeling relationships? → graph.

Key idea

The same algorithm can be fast or slow depending on the structure underneath it. Dijkstra with a plain array is O(V²); with a heap it's O(E log V). The structure is part of the algorithm.

Recap & quick check

Key takeaways

  • Arrays give O(1) index access; hash maps/sets give O(1) average lookup.
  • Stacks (LIFO) power DFS/undo; queues (FIFO) power BFS/scheduling.
  • Heaps hand you the min/max in O(1) and update in O(log n).
  • Balanced BSTs keep data sorted with O(log n) operations.
  • Picking the structure with the right costs is the essence of algorithm design.

Quick check

1. Which structure gives O(1) average lookup by key?

2. You need to repeatedly remove the smallest element. Best choice?

3. Which processing order does a stack give?

4. Why does the choice of structure matter to an algorithm?

Toolbox in hand, let's start solving. First: finding things fast. Next up: Module 6 — Searching & Binary Search.