Overview
Want to master algorithms and data structures through practical, annotated code? This by-example guide teaches essential algorithmic thinking and data structure usage through heavily annotated examples in C, Go, Python, and Java, organized by complexity level.
What Is Algorithms and Data Structures By-Example Learning?
Algorithms and data structures by-example learning is a code-first approach where you learn through annotated, runnable programs rather than abstract theory. Each example shows:
- What the code does — step-by-step annotations using
// =>notation documenting values and state - Why it works — rationale behind algorithmic choices and data structure selection
- When to use it — practical guidance on applying each concept to real problems
- Trade-offs — time and space complexity considerations explained in context
This approach is ideal for developers who want to build intuition for algorithmic problem-solving by reading and reasoning through concrete code rather than mathematical proofs.
Learning Path
The by-example tutorial guides you through 85+ examples organized into three progressive levels, from fundamental data structures to advanced algorithmic techniques.
Coverage Philosophy
This by-example guide achieves broad coverage of algorithms and data structures through annotated code examples. The focus is on practical implementation and problem-solving patterns, not exam-style theoretical analysis.
What Is Covered
- Linear data structures — arrays, linked lists, stacks, queues, deques
- Non-linear data structures — trees, heaps, graphs, tries
- Sorting algorithms — comparison-based and non-comparison-based sorting
- Searching algorithms — linear search, binary search, and tree/graph traversal
- Dynamic programming — memoization, tabulation, and classic DP patterns
- Graph algorithms — BFS, DFS, shortest paths, and spanning trees
- Hashing — hash maps, hash sets, and collision resolution strategies
What Is Not Covered
- Formal proofs of correctness or complexity (see by-concept tutorials for theoretical foundations)
- Advanced computational complexity theory (P vs NP, amortized analysis proofs)
- Specialized data structures beyond the 95% use-case threshold
Prerequisites
- Familiarity with at least one programming language
- Basic understanding of variables, loops, and functions
- Comfort reading code and following execution flow
Structure of Each Example
Every example follows a consistent five-part format:
- Brief Explanation — what the example demonstrates and why it matters (2-3 sentences)
- Mermaid Diagram — visual representation of data flow, state transitions, or structure (when appropriate)
- Heavily Annotated Code — runnable implementations in C, Go, Python, and Java with
// =>comments documenting values and outcomes - Key Takeaway — the core insight to retain from the example (1-2 sentences)
- Why It Matters — production relevance and real-world impact (50-100 words)
This structure ensures you gain both understanding and intuition from every example.
Examples by Level
Beginner (Examples 1–28)
- Example 1: Creating and Accessing Arrays
- Example 2: Modifying Arrays — Append, Insert, and Delete
- Example 3: Array Slicing
- Example 4: Iterating Over Arrays
- Example 5: Two-Dimensional Arrays
- Example 6: Singly Linked List — Structure and Traversal
- Example 7: Singly Linked List — Prepend and Search
- Example 8: Singly Linked List — Delete a Node
- Example 9: Stack Using a List — Push and Pop
- Example 10: Stack Application — Balanced Parentheses
- Example 11: Stack Application — Reverse a String
- Example 12: Queue Using collections.deque — Enqueue and Dequeue
- Example 13: Queue Application — First-Come First-Served Simulation
- Example 14: Deque — Double-Ended Operations
- Example 15: Deque Application — Palindrome Check
- Example 16: Linear Search — Unsorted Array
- Example 17: Linear Search — Finding All Occurrences
- Example 18: Big-O — O(1), O(n), and O(n²) in Practice
- Example 19: Bubble Sort
- Example 20: Selection Sort
- Example 21: Insertion Sort
- Example 22: Comparing Sorting Algorithms
- Example 23: Factorial — Recursive vs Iterative
- Example 24: Fibonacci — Naive Recursion and Memoization
- Example 25: Recursive Sum and Binary Search (Recursive)
- Example 26: Simulating Recursion with an Explicit Stack
- Example 27: Linear Search on Structured Data
- Example 28: Sorting Structured Data with a Key Function
Intermediate (Examples 29–57)
- Example 29: Binary Search on a Sorted Array
- Example 30: Binary Search for Insert Position
- Example 31: Hash Table with Chaining
- Example 32: Hash Map for Frequency Counting
- Example 33: BST Insert and Search
- Example 34: BST Inorder Traversal
- Example 35: Min-Heap with heapq
- Example 36: Max-Heap and Priority Queue with Tuples
- Example 37: Heapify and the Heap Property
- Example 38: Merge Sort Implementation
- Example 39: Quicksort with Lomuto Partition
- Example 40: Counting Sort for Integer Keys
- Example 41: AVL Tree Height and Balance Factor
- Example 42: BFS on a Binary Tree (Level-Order Traversal)
- Example 43: DFS on a Binary Tree (Iterative with Stack)
- Example 44: Divide and Conquer — Maximum Subarray
- Example 45: Backtracking — Generating Permutations
- Example 46: Two Pointers — Two Sum in Sorted Array
- Example 47: Two Pointers — Container With Most Water
- Example 48: Fixed-Size Sliding Window — Maximum Sum Subarray
- Example 49: Variable-Size Sliding Window — Longest Substring Without Repeating Characters
- Example 50: Prefix Sum Array for Range Queries
- Example 51: Adjacency List Representation
- Example 52: Adjacency Matrix Representation
- Example 53: Fibonacci with Memoisation
- Example 54: Recursion Pattern — Power Set (Subsets)
- Example 55: Sliding Window + Hash Map — Minimum Window Substring
- Example 56: Two Pointers + Sorting — Three Sum
- Example 57: BFS + Graph — Shortest Path in Unweighted Graph
Advanced (Examples 58–85)
- Example 58: Memoization — Top-Down Fibonacci
- Example 59: Tabulation — Bottom-Up Knapsack
- Example 60: Longest Common Subsequence (LCS)
- Example 61: Longest Increasing Subsequence (LIS)
- Example 62: Coin Change — Minimum Coins
- Example 63: Dijkstra's Shortest Path
- Example 64: Bellman-Ford — Shortest Path with Negative Weights
- Example 65: Floyd-Warshall — All-Pairs Shortest Paths
- Example 66: Topological Sort (Kahn's Algorithm)
- Example 67: Kruskal's Minimum Spanning Tree
- Example 68: Prim's Minimum Spanning Tree
- Example 69: Trie — Prefix Tree
- Example 70: Segment Tree — Range Sum and Point Update
- Example 71: Union-Find (Disjoint Set Union)
- Example 72: N-Queens Problem
- Example 73: Sudoku Solver
- Example 74: KMP String Search
- Example 75: Rabin-Karp Rolling Hash
- Example 76: Bit Manipulation Fundamentals
- Example 77: XOR Tricks — Finding the Unique Element
- Example 78: Monotonic Stack — Next Greater Element
- Example 79: Monotonic Deque — Sliding Window Maximum
- Example 80: Radix Sort — Non-Comparative Linear Sort
- Example 81: Amortized O(1) — Dynamic Array Doubling
- Example 82: Amortized O(1) — Splay Tree Intuition via Two-Stack Queue
- Example 83: Fenwick Tree (Binary Indexed Tree) — Prefix Sums
- Example 84: Flood Fill — Connected Component Labeling
- Example 85: A* Search — Heuristic Pathfinding
Last updated March 19, 2026