DSA Intro
Data structures are ways of organising data in memory so that the operations you need are fast. Algorithms are step-by-step procedures that solve a problem, such as sorting a list, finding the shortest route on a map or compressing a file. The two go together: the right data structure often makes an algorithm simple, and a clever algorithm is often just a smart way of walking through a data structure.
This course teaches the classic structures and algorithms that every programmer meets sooner or later, in interviews, in libraries and in real systems. Each implementation is written in Python and runs in your browser, and many lessons include a step-through animation of the algorithm on the same data as the code.
Why learn DSA?
| Reason | Example |
|---|---|
| Write faster programs | Finding a name among 1,000,000 sorted names takes about 20 checks with binary search instead of up to 1,000,000 |
| Choose the right tool | A queue for a print spooler, a hash map for a phone book, a graph for a road network |
| Understand libraries | Python's sorted(), dict and heapq are DSA you use every day |
| Solve new problems | Many puzzles become easy once you recognise them as "shortest path" or "dynamic programming" |
| Interviews | Most technical interviews test these ideas directly |
Data structures you will meet
# A few built-in Python structures and what they are good at
scores = [72, 95, 64, 88] # array/list: ordered, indexed
stack = ["page1", "page2"] # stack: last in, first out
phone = {"Ada": "555-0101", "Linus": "555-0199"} # hash map: key -> value
seen = {"red", "green"} # hash set: fast membership tests
print(scores[1]) # direct access by index
stack.append("page3")
print(stack.pop()) # the most recent item comes out first
print(phone["Ada"]) # look up by key
print("blue" in seen) # membership test95
page3
555-0101
False
Later lessons build these structures yourself (linked lists, stacks, queues, hash tables, trees, heaps, graphs) so you understand what happens inside.
An algorithm in action
An algorithm must be precise (every step is clear), finite (it stops) and correct (it gives the right answer for every valid input). Here is one that finds the largest value in a list:
def find_max(values):
largest = values[0]
for v in values[1:]:
if v > largest:
largest = v
return largest
print(find_max([12, 47, 3, 28, 47, 9]))
print(find_max([-5, -2, -9]))47
-2
Press Next below to watch a simple sorting algorithm, bubble sort, rearrange a small array. The highlighted bars are being compared; green bars are in their final place.
Measuring algorithms
Two algorithms can solve the same problem with very different amounts of work. We usually count the number of basic operations (comparisons, swaps) as a function of the input size n, and describe the growth with Big O notation, for example O(n) or O(n²). The Time Complexity section covers this in detail; for now, here is the idea:
def count_pairs_compared(n):
comparisons = 0
for i in range(n):
for j in range(i + 1, n):
comparisons += 1 # compare item i with item j
return comparisons
for n in [10, 100, 1000]:
print(f"n = {n:>4}: one pass = {n:>4} steps, all pairs = {count_pairs_compared(n):>6} steps")n = 10: one pass = 10 steps, all pairs = 45 steps
n = 100: one pass = 100 steps, all pairs = 4950 steps
n = 1000: one pass = 1000 steps, all pairs = 499500 steps
Ten times more data means ten times more work for a single pass, but about a hundred times more work for comparing all pairs. That difference is what makes some programs feel instant and others hang.
How this course is organised
- Arrays, sorting and searching: the foundation, with eight classic algorithms.
- Linked lists, stacks, queues and hash tables: linear structures and constant-time lookups.
- Trees and graphs: hierarchical and networked data, traversals, shortest paths, spanning trees and maximum flow.
- Time complexity: how to reason about speed with Big O.
- Algorithm design: greedy algorithms, dynamic programming, backtracking and more.
Tip: Change the data in the playgrounds and rerun them. Predict the output first, then check. That habit builds intuition faster than reading.
You only need basic Python (variables, lists, loops, functions). If those are new, take the Python course first.
Going further: the Algorithm Theory track proves why these algorithms are correct and how fast they are, from loop invariants and recurrences to graduate topics such as randomized, approximation and streaming algorithms.
Test yourself
What is an algorithm?
- A way of storing data in memory
- A precise, finite sequence of steps that solves a problem
- A programming language
- A kind of computer hardware
Which Python structure gives fast lookup of a value by key?
- list
- tuple
- dict (hash map)
- str
If the input grows 10 times larger, roughly how much more work does an algorithm that compares all pairs do?
- The same
- 10 times
- About 100 times
- 1000 times