What Are Data Structures and Algorithms
Why the way you store data decides how fast your program runs, and how to start thinking about both.
Starting from zero
If you have written a few programs, you have already used data structures and algorithms without naming them. An array is a data structure. A loop that finds the largest number is an algorithm. This lesson gives those instincts names so you can reason about them deliberately.
A data structure is a way of organising data in memory. An algorithm is a sequence of steps that transforms input into output. They are inseparable in practice — choosing a different data structure often changes which algorithms are even possible.
A concrete example
Suppose you have ten thousand student roll numbers and you need to check whether a specific roll number exists.
Approach one — a plain list. You check each entry one at a time. In the worst case you look at all ten thousand.
def exists_in_list(roll_numbers, target):
for number in roll_numbers:
if number == target:
return True
return False
Approach two — a set. A set stores values in a hash table, so it can jump almost directly to the right slot.
def exists_in_set(roll_numbers, target):
return target in roll_numbers # roll_numbers is a set
Same question, same data, same answer. The second version does roughly one step instead of ten thousand. Nothing about the algorithm got clever — the data structure changed, and that changed everything.
This is the central lesson of the whole subject. Most large speedups come from restructuring data, not from micro-optimising code.
The two costs you always trade
Every choice you make is measured against two resources:
- Time — how many operations run as the input grows.
- Space — how much extra memory you need beyond the input.
The set in our example is faster but uses extra memory to store the hash table. That is a space-time tradeoff, and it appears constantly. There is rarely a universally best answer, only the best answer for your constraints.
How to think about a new problem
When you meet an unfamiliar problem, work through these questions in order:
- What exactly is the input and output? Write down a small example by hand.
- What is the simplest correct solution? Brute force is a valid starting point and proves you understand the problem.
- Where does it waste work? Look for repeated computation or repeated scanning.
- Which data structure removes that waste? A hash map removes repeated searching. Sorting removes repeated comparison. A heap removes repeated finding-the-minimum.
- What did the change cost? Confirm the extra memory or setup time is acceptable.
Skipping straight to step four is the most common mistake beginners make. Solve it slowly first, then make it fast.
The structures you will learn
Over this course you will build up the following toolkit:
| Structure | Good at | Weak at |
|---|---|---|
| Array | Index access, cache locality | Insert and delete in the middle |
| Linked list | Insert and delete at a known node | Random access by index |
| Hash map | Lookup, insert, delete by key | Ordering, worst-case guarantees |
| Stack | Undo, backtracking, nesting | Accessing anything but the top |
| Queue | Fair ordering, breadth-first search | Accessing the middle |
| Tree | Hierarchies, sorted ranges | Extra memory and pointer overhead |
| Graph | Relationships and networks | Complex traversal logic |
| Heap | Repeatedly finding min or max | Searching for an arbitrary value |
You do not need to memorise this now. Come back to it once you have met each structure — it becomes a genuinely useful reference.
What comes next
The next lesson covers time and space complexity, the notation used to compare two approaches without running them. It is the vocabulary every later lesson relies on, so it is worth taking slowly.
Summary
Data structures organise data, algorithms process it, and the two decisions are linked. Always write the simple solution first, find the wasted work, then pick the structure that eliminates it. Judge every change against both time and memory.