Phase 7 · MasteryModule 31~32 min read

Choosing the Right Structure

Bring it all together: a decision framework, the master Big-O cheat sheet, and common interview patterns.

What you'll learn

You've met every major data structure. The real skill is choosing — matching the structure to what your program does most. This capstone gives you a decision guide, the master complexity cheat sheet, and the patterns that turn all of this into problem-solving.

A decision guide

Start from what you need to do most often, then reach for the structure built for it:

If you need…

Fast access by index / position

→Array (dynamic array)· O(1) indexing

If you need…

Fast insert & delete at the ends

→Linked list / Deque· O(1), no shifting

If you need…

Fast key → value lookup, no order

→Hash table· O(1) average

If you need…

Sorted data, ranges, ordered iteration

→Balanced BST / B-tree· O(log n), keeps order

If you need…

Always grab the min or max

→Heap / Priority queue· O(1) peek, O(log n) update

If you need…

Prefix search / autocomplete

→Trie· O(L) in the word length

If you need…

Model relationships / networks

→Graph· Vertices + edges

If you need…

Track connected groups / merging

→Union-Find· ~O(1) per operation

The master cheat sheet

Typical (average-case) complexities for the operations each structure is used for:

StructureAccessSearchInsertDeleteSpace
Array / dynamic arrayO(1)O(n)O(1)* endO(n)O(n)
Linked listO(n)O(n)O(1) endO(1)* endO(n)
Stack / QueueO(n)O(n)O(1)O(1)O(n)
Hash table—O(1)O(1)O(1)O(n)
Balanced BST—O(log n)O(log n)O(log n)O(n)
HeapO(1) peekO(n)O(log n)O(log n)O(n)
Trie—O(L)O(L)O(L)O(Σ·nodes)
B-tree—O(log n)O(log n)O(log n)O(n)
Graph (adj list)—O(V+E)O(1) edgeO(E)O(V+E)

Note

* amortized. "Search" on a hash table means by key; on a heap it's O(n) because a heap only orders parent-vs-child, not siblings. L = word length, Σ = alphabet size.

Interview patterns

Most coding problems are a structure plus a well-known technique:

PatternReach forTypical problem
Fast lookups / dedupHash set / mapTwo-sum, count distinct
Top-K / streaming maxHeapK largest, median of a stream
Shortest path / levelsBFS (+ queue)Maze, word ladder
Explore / backtrackDFS (+ stack/recursion)Permutations, islands
Ordered rangesBalanced BST / sortingInterval problems
PrefixesTrieAutocomplete, word search
Grouping / connectivityUnion-FindNumber of connected components

Why it all matters

Everything comes back to growth. The right structure turns an O(n) or O(n²) program into O(log n) or O(1) — the difference between instant and unusable at scale. One last look at what that means as n grows:

The payoff, one more time
ops0input size n →
n = 8
ComplexityNameOperations at n = 8
O(1)Constant1
O(log n)Logarithmic3
O(n)Linear8
O(n log n)Linearithmic24
O(n²)Quadratic64
O(2ⁿ)Exponential256
O(n!)Factorial40,320
Choosing O(log n) over O(n²) isn't a micro-optimization — it's the difference between shipping and not.

Where to go next

  • Practice on real problems — implement each structure once from scratch, then solve with them.
  • Advanced topics: string algorithms (KMP, suffix arrays), computational geometry, advanced graph algorithms (max-flow, SCCs), and persistent data structures.
  • Systems: see these structures in the wild — B+ trees in databases, LSM-trees in key-value stores, tries in routers, heaps in schedulers.

Key idea

You've gone from arrays to balanced trees, hashing, and graphs — the toolkit behind every serious program. The goal was never to memorize them, but to see how each one works and reach for the right one by instinct. You can now do exactly that.

Recap & quick check

Key takeaways

  • Pick a structure by what your program does most: index, search by key, keep order, get the min, match prefixes, model relationships.
  • Hash tables give O(1) average lookup but no order; balanced trees give O(log n) with order.
  • Heaps give instant min/max; tries give prefix search; union-find tracks connectivity.
  • Most interview problems are a structure plus a pattern (hashing, BFS/DFS, heap, two pointers).
  • The right choice can turn O(n²) into O(log n) — that's why data structures matter.

Quick check

1. You need O(1) average lookup by key, and order doesn't matter. Use:

2. You must repeatedly extract the smallest element. Use:

3. You need sorted order AND fast range queries. Use:

4. Which pattern fits 'find the shortest path in an unweighted maze'?

🎉 That's the whole course. You've mastered data structures and the algorithms that bring them to life — from your first array to self-balancing trees and graph algorithms. Go build something fast.