Data Structures & Algorithms

Searching, sorting, linked lists, stacks, queues, hash tables, trees, heaps, graphs, backtracking and dynamic programming, in Python. Watch every comparison, swap, pointer and recursive call as it happens: the visual way to prepare for coding interviews.

Python · intermediate · 25 lessons · 3 h 6 min · free

What you'll learn

Before you start

Lessons

Complexity and Searching

Big O, linear and binary search, two pointers and sliding windows.

  1. Big O: Counting the Work: Count how many steps a single loop and a nested loop take for the same list. One grows with n, the other with n × n.
  2. Linear Search: Check every item in turn until you find the target, or reach the end.
  3. Binary Search: In a sorted list, look at the middle and throw away the half that cannot contain the target. Ten items take only three checks.
  4. Two Pointers: Find two numbers that add up to a target in a sorted list, with one pointer at each end moving towards the other.
  5. Sliding Window: Find the biggest sum of 3 numbers in a row by sliding a window along: add the new number, drop the old one.

Sorting

Bubble, selection, insertion, merge and quick sort.

  1. Bubble Sort: Compare neighbours and swap them when they are out of order. After each pass the biggest unsorted value has bubbled to the end.
  2. Selection Sort: Find the smallest remaining value and swap it into the next place. One swap per pass.
  3. Insertion Sort: Take the next value and slide it left into its place among the already sorted ones, like sorting cards in your hand.
  4. Merge Sort: Split the list in half until each piece has one item, then merge the sorted pieces back together. Divide and conquer.
  5. Quick Sort: Pick a pivot, split the rest into smaller and larger, and sort each side the same way.

Lists, Stacks, Queues and Hashing

Linked lists, stacks, queues and hash tables.

  1. Linked Lists: Chain nodes together with next pointers, add to the front in O(1), and walk the chain from head to the end.
  2. Reversing a Linked List: The classic interview question: turn 1 → 2 → 3 into 3 → 2 → 1 by flipping each arrow, with three pointers.
  3. Stacks: Balanced Brackets: Push every opening bracket, and pop when a closing one arrives. A stack checks nesting perfectly.
  4. Queues: First in, first out: people join at the back and are served from the front. A deque does both ends in O(1).
  5. How a Hash Table Works: Build a tiny dictionary: a hash function picks a bucket for each key. "cat" and "act" land in the same bucket: a collision.
  6. Two Sum with a Hash Map: Find two numbers that add up to the target in one pass, by remembering what you have seen in a dictionary.

Trees and Heaps

Traversals, binary search trees and priority queues.

  1. Tree Traversal: Visit every node of a binary tree in order: left subtree, the node, right subtree. Recursion does the bookkeeping.
  2. Binary Search Trees: Insert keys so that smaller ones go left and bigger ones go right, then find a key by walking down one path.
  3. Heaps and Priority Queues: Push tasks with priorities and always get the most urgent one first, using Python's heapq.

Graphs

Breadth-first search, depth-first search and shortest paths.

  1. Breadth-First Search: Explore a graph level by level with a queue of paths, and find the shortest route from A to E.
  2. Depth-First Search: Go as deep as you can along one path before backing up. Recursion keeps track of where to come back to.
  3. Dijkstra's Shortest Paths: Find the cheapest route to every node in a weighted graph, always expanding the closest node next with a heap.

Backtracking and Dynamic Programming

Exploring every choice, and reusing answers to subproblems.

  1. Backtracking: All Subsets: For each item, try including it, then try leaving it out. Undo each choice on the way back. The result is every subset.
  2. Dynamic Programming: Memo and Table: Fibonacci without the waste: remember answers in a dict (top down), or fill a table from the bottom up.
  3. Dynamic Programming: Coin Change: The fewest coins that make 6 with coins 1, 3 and 4. Greedy says 4 + 1 + 1; dynamic programming finds 3 + 3.

Related courses