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.

Tutorial·Difficulty: Intermediate·12 chapters·Updated Apr 19, 2026

Chapters

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

  1. Introduction: What algorithms are, why they matter, first measured example
  2. Big-O: Time and space complexity, common classes, amortized analysis

Core: Data Structures

  1. Arrays and Hashing: Arrays, hash tables, O(1) lookup patterns
  2. Linked Lists: Singly, doubly, cycle detection, two-pointer tricks
  3. Searching: Linear, binary search, binary search on the answer
  4. Sorting: Bubble, insertion, merge, quick, heap, counting, when each fits
  5. Trees: Binary trees, BSTs, traversals, balanced trees, heaps
  6. Graphs: Representations, BFS, DFS, Dijkstra, topological sort, Union-Find

Advanced

  1. Dynamic Programming: Memoization, tabulation, classic DP problems
  2. Greedy and Divide-and-Conquer: When each works

Ecosystem

  1. Patterns: Two pointers, sliding window, backtracking, prefix sums, monotonic stack

Mastery

  1. Best Practices: Approaching problems, interview prep, anti-patterns

How to Use This Tutorial

  1. Read sequentially. Each chapter builds on earlier ones
  2. Code every example. Algorithms stick when your fingers type them
  3. 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
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)

  1. Chapters 1 to 8 for the core toolkit
  2. Chapter 11 for patterns
  3. Skip DP and greedy unless your work touches them

Interview prep (roughly 30 hours)

  1. All 12 chapters
  2. 50+ problems on LeetCode, sorted by topic
  3. Two mock interviews before the real one

Learning for learning's sake (roughly 20 hours)

  1. All chapters
  2. Implement each data structure from scratch
  3. Read CLRS (Cormen et al.) for the proofs; use this tutorial for the intuition

Additional Resources

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.