Tutorial
Algorithms Tutorial
A practical tutorial on algorithms and data structures using Python. Covers Big-O, sorting, searching, hashing, linked lists, trees, graphs, dynamic programming, greedy, divide-and-conquer, and the patterns that show up on every interview whiteboard.
Chapters
01
Introduction: Why Algorithms Matter
02
Big-O: Measuring What Your Code Costs
03
Arrays and Hashing: The Everyday Heroes
04
Linked Lists: Pointers and Two-Pointer Tricks
05
Searching: Finding Things Fast
06
Sorting: Beyond sort()
07
Trees: Hierarchies and Heaps
08
Graphs: Nodes, Edges, and the Algorithms That Navigate Them
09
Dynamic Programming: Memoize Your Way to Fast Solutions
10
Greedy and Divide-and-Conquer: Two Useful Hammers
11
Patterns: Two Pointers, Sliding Window, and Friends
12
Best Practices: Approaching Problems and Interviews
About this tutorial
A practical tour of algorithms and data structures using Python, from Big-O to dynamic programming to the patterns that decide interviews.
Who This Is For
- Developers who can write code but never formally studied computer science
- Engineers preparing for technical interviews who want more than pattern matching
- Anyone tired of copy-pasting sorting from Stack Overflow without understanding why the chosen one fits
Contents
Fundamentals
- Introduction: What algorithms are, why they matter, first measured example
- Big-O: Time and space complexity, common classes, amortized analysis
Core: Data Structures
- Arrays and Hashing: Arrays, hash tables, O(1) lookup patterns
- Linked Lists: Singly, doubly, cycle detection, two-pointer tricks
- Searching: Linear, binary search, binary search on the answer
- Sorting: Bubble, insertion, merge, quick, heap, counting, when each fits
- Trees: Binary trees, BSTs, traversals, balanced trees, heaps
- Graphs: Representations, BFS, DFS, Dijkstra, topological sort, Union-Find
Advanced
- Dynamic Programming: Memoization, tabulation, classic DP problems
- Greedy and Divide-and-Conquer: When each works
Ecosystem
- Patterns: Two pointers, sliding window, backtracking, prefix sums, monotonic stack
Mastery
- Best Practices: Approaching problems, interview prep, anti-patterns
How to Use This Tutorial
- Read sequentially. Each chapter builds on earlier ones
- Code every example. Algorithms stick when your fingers type them
- Solve adjacent problems. For each chapter, do 3 to 5 related problems on LeetCode, HackerRank, or similar
Quick Reference
Setup
python3 --version # 3.11+ recommended
python3 -m pip install ipython # optional; nice REPL
Hello Algorithm (Linear Search)
def find(items: list[int], target: int) -> int:
for i, item in enumerate(items):
if item == target:
return i
return -1
print(find([5, 3, 7, 1], 7)) # 2
Common Patterns
# Binary search (sorted input)
def bsearch(arr, target):
lo, hi = 0, len(arr) - 1
while lo <= hi:
mid = (lo + hi) // 2
if arr[mid] == target: return mid
if arr[mid] < target: lo = mid + 1
else: hi = mid - 1
return -1
# Two pointers
def two_sum_sorted(arr, target):
lo, hi = 0, len(arr) - 1
while lo < hi:
s = arr[lo] + arr[hi]
if s == target: return (lo, hi)
if s < target: lo += 1
else: hi -= 1
return None
# Hash lookup (the everyday O(1) trick)
def two_sum(arr, target):
seen = {}
for i, x in enumerate(arr):
if target - x in seen:
return (seen[target - x], i)
seen[x] = i
# BFS on a graph
from collections import deque
def bfs(graph, start):
seen = {start}
queue = deque([start])
while queue:
node = queue.popleft()
for nb in graph[node]:
if nb not in seen:
seen.add(nb)
queue.append(nb)
return seen
Big-O Cheat Sheet
Operation List Dict Set Deque
index access O(1) - - O(n)
append O(1)* - - O(1)
insert at front O(n) - - O(1)
lookup O(n) O(1)* O(1)* O(n)
in-place sort O(n log n) - - -
*amortized
Common totals:
O(1) hash lookup, stack push, array index
O(log n) binary search, balanced tree operations
O(n) linear scan, most list methods
O(n log n) efficient sorts (merge, heap, introsort)
O(n^2) nested loops, bubble sort, most naive solutions
O(2^n) brute force subsets, some DP without memoization
O(n!) permutations, brute force traveling salesman
Learning Path Suggestions
Daily coding fluency (roughly 12 hours)
- Chapters 1 to 8 for the core toolkit
- Chapter 11 for patterns
- Skip DP and greedy unless your work touches them
Interview prep (roughly 30 hours)
- All 12 chapters
- 50+ problems on LeetCode, sorted by topic
- Two mock interviews before the real one
Learning for learning's sake (roughly 20 hours)
- All chapters
- Implement each data structure from scratch
- Read CLRS (Cormen et al.) for the proofs; use this tutorial for the intuition
Additional Resources
- CLRS: Introduction to Algorithms: the canonical textbook
- Competitive Programmer's Handbook (Laaksonen): free, compact, competitive-programming flavored
- Algorithms, 4th Edition by Sedgewick and Wayne: Java-based, with good visualisations
- LeetCode: problems, graded by difficulty and topic
- NeetCode: curated LeetCode practice roadmap
- Visualgo: interactive visualisations of classic algorithms
Python Version
This tutorial is written for Python 3.11+. Type hints use modern syntax (list[int], dict[str, int]). Everything works on 3.10 with small tweaks.