Skip to main content

Algorithms & Data Structures Overview

This section covers fundamental algorithms and data structures essential for technical interviews and competitive programming.

What You’ll Learn

This comprehensive guide covers:
  • Sorting Algorithms: Comparison-based and non-comparison-based sorting techniques
  • Search Algorithms: Binary search and its variations
  • Dynamic Programming: Problem-solving patterns and optimization techniques
  • Graph Algorithms: Traversal, shortest paths, and connectivity
  • Data Structures: Arrays, linked lists, stacks, queues, trees, and graphs

Time Complexity Overview

Understanding algorithm efficiency is crucial:
Complexity ClassAlgorithms
O(n²)Bubble Sort, Selection Sort, Insertion Sort
O(n log n)Quick Sort, Merge Sort, Heap Sort
O(n^1.3)Shell Sort
O(n)Counting Sort, Bucket Sort, Radix Sort

Stability in Sorting

Stable algorithms maintain the relative order of equal elements:
  • ✅ Stable: Bubble Sort, Insertion Sort, Merge Sort, Radix Sort
  • ❌ Unstable: Selection Sort, Quick Sort, Shell Sort, Heap Sort
Stability Definition: After sorting, two equal keys maintain their original order. For example, if we have two records with the same key value, a stable algorithm ensures the one that appeared first in the input will appear first in the output.

Algorithm Categories

Comparison-Based Sorts

  1. Bubble Sort: Repeatedly swap adjacent elements if they’re in wrong order
  2. Selection Sort: Find minimum element and place it at the beginning
  3. Insertion Sort: Build sorted array one element at a time
  4. Heap Sort: Use max heap to extract elements in order
  5. Merge Sort: Divide array into halves, sort recursively, then merge
  6. Quick Sort: Select pivot, partition around it, recursively sort partitions

Non-Comparison Sorts

  1. Counting Sort: Count occurrences of each element
  2. Bucket Sort: Distribute elements into buckets, sort each bucket
  3. Radix Sort: Sort by individual digits/characters

Special Algorithms

  1. Shell Sort: Generalized insertion sort with gap sequences

Common Data Structures

Linear Structures

  • Arrays: Fixed-size, contiguous memory
  • Linked Lists: Dynamic size, non-contiguous nodes
  • Stacks: LIFO (Last In First Out) operations
  • Queues: FIFO (First In First Out) operations

Tree Structures

  • Binary Trees: Each node has at most two children
  • Binary Search Trees: Left subtree < root < right subtree
  • Heaps: Complete binary tree with heap property

Graph Structures

  • Adjacency Matrix: 2D array representation
  • Adjacency List: Array of lists representation

Problem-Solving Patterns

Two Pointers

Useful for array/string problems requiring O(n) time:
int left = 0, right = n - 1;
while (left < right) {
    // Process elements
    if (condition) left++;
    else right--;
}

Sliding Window

For subarray/substring problems:
for (int i = 0, j = 0; i < n; i++) {
    // Expand window
    while (condition) {
        // Shrink window
        j++;
    }
}

Monotonic Stack

Find next greater/smaller element:
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
    while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
        // Process elements
        stack.pop();
    }
    stack.push(i);
}

Interview Tips

Time Complexity

Always analyze and state the time complexity of your solution

Space Complexity

Consider space-time tradeoffs and optimize when possible

Edge Cases

Test with empty inputs, single elements, and boundary conditions

Code Quality

Write clean, readable code with meaningful variable names

Common Pitfalls

  • Off-by-one errors: Carefully check loop boundaries
  • Integer overflow: Use long for large number operations
  • Null pointer exceptions: Always validate input
  • Infinite loops: Ensure loop conditions eventually become false

Next Steps

Explore each topic in depth:

Sorting Algorithms

Learn all major sorting algorithms with implementations

Binary Search

Master binary search and its variations

Dynamic Programming

Solve optimization problems efficiently

Graph Theory

Understand graph algorithms and applications

Build docs developers (and LLMs) love