Applying Data Structures and Algorithms to Data Systems
This lesson explains how the data structures and algorithms of computer science underpin databases, query engines and pipelines, and help data engineers predict how operations perform at scale.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the data structures behind indexes, joins and caches.
- Compare operations by their complexity.
- Model pipelines as directed acyclic graphs.
Data structures in data systems
Familiar data structures explain how data systems behave. Database indexes are usually B-trees, which keep keys sorted so that lookups and range queries are fast. Hash tables underlie hash joins, where one table is loaded into a hash table and the other is probed against it, and hash partitioning, which spreads rows across nodes by key. Graphs represent pipelines as DAGs, data lineage, and vector indexes such as HNSW. Trees represent hierarchies and nested documents, and queues and stacks buffer work.
A Bloom filter is a compact structure that can say a value is definitely not present, or might be. Parquet files and databases use them to skip files or blocks that cannot contain a searched value. The following table summarises where each structure appears.
| Structure | Used in |
|---|---|
| B-trees | Database indexes |
| Hash tables | Hash joins and hash partitioning |
| Graphs | Pipelines as DAGs, data lineage, vector indexes such as HNSW |
| Trees | Hierarchies and nested documents |
| Queues and stacks | Buffering work |
| Bloom filters | Skipping files or blocks that cannot contain a value |
Data structures
Complexity
Complexity describes how the cost of an operation grows with the amount of data, n. A hash lookup takes about the same time however large the table, O(1). A B-tree index lookup grows very slowly, O(log n), because each level of the tree narrows the search. Sorting grows slightly faster than the data, O(n log n). A nested loop join compares every row of one table with every row of the other, O(n × m), so joining two tables of a million rows means a trillion comparisons. The following table summarises the typical complexities.
| Operation | Complexity |
|---|---|
| Hash lookup | O(1) on average |
| Index (B-tree) lookup | O(log n) |
| Sorting | O(n log n) |
| Nested loop join | O(n × m) |
In the cloud, inefficient algorithms directly increase compute cost.
The following diagram compares the number of comparisons needed to check records against a list and against a set.

Complexity
Pipelines as graphs
Orchestrators such as Apache Airflow and AWS Step Functions model pipelines as directed acyclic graphs (DAGs). Each step is a node, each dependency is a directed edge from a step to the steps that depend on it, and the absence of cycles guarantees that the pipeline can finish, because no step can end up waiting for itself.
Two properties of DAGs matter in practice. A topological order of the graph, an ordering in which every step comes after all the steps it depends on, gives a valid sequence in which to run the steps. And steps with no dependency between them, independent branches of the graph, can run in parallel, which shortens the pipeline's total running time.
The following diagram shows a small pipeline as a DAG.

A lookup that grows quadratically
A Python Lambda function matches each of 200,000 incoming records against a list of 50,000 blocked account IDs, using if account in blocked_list. It times out as the list grows. The following table compares the two approaches.
| Approach | Cost per lookup | Total comparisons |
|---|---|---|
| Search a list | O(n) | About 10 billion |
| Look up in a set (hash table) | O(1) on average | About 200,000 |
Converting the list to a set once, outside the handler, makes the function finish in under a second. The same principle explains why databases use hash joins and indexes instead of nested loops.
How do Airflow and Step Functions model a pipeline?
Show answer
As a directed acyclic graph (DAG) of tasks and their dependencies.
Exam Tips
Remember for the exam
- Database indexes are B-trees; hash joins use hash tables.
- Airflow and Step Functions model pipelines as DAGs.
- Sorting is O(n log n); hash lookups are O(1) on average.
- HNSW vector indexes are graphs.
- Avoid nested loop joins on large tables.