Data Structures · Lesson 1 of 8
What Are Data Structures?
Why structure matters, abstract data types, and how interviews use them.
- Beginner
- 10 min read
- 3 objectives
What you will learn
- Define data structure vs algorithm
- Name common ADTs
- Connect to real code (lists, maps)
A data structure is a way of organizing data in memory so that the operations you care about are fast enough. An algorithm is the sequence of steps that uses the structure to solve a problem. The two go together: the right structure often makes the algorithm trivial, and the wrong one turns a feature that should be instant into a timeout.
Organising data is a design decision
Imagine a library. If books are thrown into one giant pile, finding a title means checking every book. If they are shelved alphabetically, you can jump straight to the right shelf. The books did not change, only how they are organised, and the speed of finding one changed enormously. A data structure is a way of organising data in memory so that the operations you care about (add, find, remove, order) are fast.
There is no single best structure. Each one is good at some operations and worse at others, so choosing well is a skill. That skill is what interviews test and what makes real programs fast.
Why it matters
Imagine finding a contact among one million. In an unsorted list you may have to look at every entry, about a million steps. In a hash table you jump straight to it, about one step. The data is identical; only the structure changed.
The toolbox
- Array / list: ordered items with fast access by position.
- Linked list: items chained by pointers, with fast insert and remove at a known spot.
- Stack: last in, first out (undo history, function calls).
- Queue: first in, first out (task scheduling, breadth-first search).
- Hash table: key to value lookup in about constant time.
- Tree: hierarchical data with fast ordered search (file systems, databases).
- Graph: things connected to other things (maps, social networks, dependencies).
Abstract data types
An abstract data type (ADT) says what operations exist, not how they are built. A stack promises push and pop; you could build it on an array or a linked list. Thinking in ADTs lets you pick the behavior you need first and the implementation second.
You already use them
stack = []
stack.append("a") # list used as a stack
stack.append("b")
print(stack.pop())
lookup = {"id": 7} # dict is a hash table
print(lookup["id"])b 7
How to choose
Ask four questions: What operations will I do most often? Does order matter? Are duplicates allowed? How large can the data get? The rest of this course gives you the vocabulary (and the Big O lesson gives you the measuring stick) to answer them.
The same task, three structures
Suppose you need to check whether a username is taken. Watch how the choice of structure changes the work, even though the answer is identical.
import time
names_list = [f"user{i}" for i in range(200_000)]
names_set = set(names_list)
target = "user199999" # worst case for the list
t = time.perf_counter()
found_list = target in names_list # scans item by item
list_time = time.perf_counter() - t
t = time.perf_counter()
found_set = target in names_set # jumps straight to the answer
set_time = time.perf_counter() - t
print(found_list, found_set)
print("set is faster:", set_time < list_time)True True set is faster: True
Both return True, but the list looked at up to 200,000 items while the set went almost directly to the answer. That gap grows with the data, which is why this matters.
How to choose: three questions
- What do I do most? Look things up by key (hash table), keep things sorted (tree), process in arrival order (queue), undo steps (stack)?
- Does the order matter? Lists keep order; sets do not.
- Do duplicates matter? Sets remove them; lists keep them.
A first mapping to real problems
- Browser back button → stack.
- Print jobs waiting their turn → queue.
- Contacts looked up by name → hash table (dict).
- File system folders → tree.
- Friends and maps → graph.
Key takeaways
- A data structure is a way of organising data so certain operations are fast.
- No structure wins everywhere; choose by the operations you need most.
- Sets and dicts make lookups fast; lists keep order; stacks and queues control processing order.
# Write your solution here
