Algorithm Analysis
Algorithm Analysis
1. Introduction
2. Algorithm Analysis
○ Big O Notation
○ Time and Space Complexity
○ Amortized Analysis
3. Data Structures
○ Arrays
○ Linked Lists
○ Stacks and Queues
○ Trees
○ Graphs
○ Hash Tables
○ Heaps (Priority Queues)
○ Tries (Prefix Trees)
○ Union-Find (Disjoint Set)
○ Bloom Filters
4. Algorithms
○ Sorting Algorithms
○ Searching Algorithms
○ Recursion and Backtracking
○ Dynamic Programming
○ Greedy Algorithms
○ Divide and Conquer
○ Graph Algorithms
■ Traversal (DFS, BFS)
■ Shortest Path (Dijkstra's, Bellman-Ford)
■ Minimum Spanning Tree (Kruskal's, Prim's)
■ Topological Sorting
○ String Algorithms
■ Pattern Matching (KMP, Rabin-Karp)
■ Suffix Trees and Arrays
5. Advanced Topics
○ Complexity Classes (P, NP, NP-Complete)
○ Parallel Algorithms
○ Cache and Memory Optimization
6. Common Problem-Solving Patterns
7. Coding Interview Strategies
8. Practice Problems with Solutions
9. Additional Resources
1. Introduction
Data Structures and Algorithms are essential for efficient problem-solving in computer science.
A strong grasp of these concepts allows you to write optimized code and demonstrates your
ability to think critically during technical interviews.
2. Algorithm Analysis
Big O Notation
Definition:
● Big O Notation describes the upper bound of an algorithm's running time or space
requirements in terms of input size (n).
● It provides a way to compare the efficiency of different algorithms.
Rules:
Example:
// O(n)
void printAllElements(int[] array) {
for (int element : array) {
System.out.println(element);
}
}
Time and Space Complexity
Why Important:
Amortized Analysis
Example:
3. Data Structures
Arrays
Definition:
Characteristics:
Operations:
● Access: O(1)
● Search:
○ Linear Search: O(n)
○ Binary Search (sorted array): O(log n)
● Insertion/Deletion:
○ At end: O(1)
○ At beginning or middle: O(n)
Dynamic Arrays:
Example in Java:
Interview Tips:
Linked Lists
Definition:
● A linear data structure where each element (node) contains data and a reference (link) to
the next node.
Types:
Operations Complexity:
● Access by Index: O(n)
● Search: O(n)
● Insertion/Deletion:
○ At head: O(1)
○ At tail: O(1) if tail reference is maintained; otherwise O(n)
○ At middle: O(n)
java
Copy code
class ListNode {
int data;
ListNode next;
ListNode(int data) {
this.data = data;
this.next = null;
}
}
Common Operations:
1.
Detecting a Cycle (Floyd's Tortoise and Hare Algorithm)
2.
Interview Tips:
Stacks
Implementation in Java:
Stack<Integer> stack = new Stack<>();
stack.push(10);
int topElement = stack.pop();
Use Cases:
Queues
Implementation in Java:
Use Cases:
Interview Tips:
Trees
Binary Trees
● Definition: A tree data structure where each node has at most two children, referred to
as the left child and the right child.
Traversal Methods:
In-order Traversal (Left, Root, Right): Used to retrieve data from BST in sorted order.
1.
Pre-order Traversal (Root, Left, Right): Used to create a copy of the tree.
2.
3.
Level-order Traversal (Breadth-First): Uses a queue to traverse nodes level by level.
java
Copy code
void levelOrder(TreeNode root) {
if (root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.remove();
System.out.print(node.data + " ");
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
4.
class TreeNode {
int data;
TreeNode left, right;
TreeNode(int data) {
this.data = data;
left = right = null;
}
}
Search Operation:
Insert Operation:
java
Copy code
TreeNode insert(TreeNode root, int key) {
if (root == null) return new TreeNode(key);
if (key < root.data)
root.left = insert(root.left, key);
else if (key > root.data)
root.right = insert(root.right, key);
return root;
}
Interview Tips:
Definition:
Operations Complexity:
● Insert: O(log n)
● Extract Max/Min: O(log n)
● Find Max/Min: O(1)
Heapify Operation:
if (largest != i) {
int temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
maxHeapify(heap, size, largest);
}
}
Use Cases:
Hash Tables
Definition:
● Data structure that maps keys to values using a hash function to compute an index into
an array of buckets or slots.
Characteristics:
Hash Function:
Example in Java:
Interview Tips:
Graphs
Definition:
Types:
Representations:
1. Adjacency Matrix:
○ 2D array of size VxV (V: number of vertices).
○ Space Complexity: O(V²)
○ Efficient for dense graphs.
2. Adjacency List:
○ An array of lists.
○ Space Complexity: O(V + E) (E: number of edges)
○ Efficient for sparse graphs.
3.
Applications:
Interview Tips:
Definition:
● A tree-like data structure used to store associative data structures, typically strings.
Characteristics:
class TrieNode {
TrieNode[] children = new TrieNode[26]; // For lowercase English
letters
boolean isEndOfWord;
TrieNode() {
isEndOfWord = false;
}
}
Operations:
Insertion:
Search:
2.
Applications:
● Auto-completion features.
● Spell checkers.
● IP routing.
Interview Tips:
Definition:
● A data structure that keeps track of elements partitioned into disjoint subsets.
● Supports two operations:
○ Find: Determine which subset an element is in.
○ Union: Merge two subsets into a single subset.
Implementation:
Example:
class UnionFind {
int[] parent;
int[] rank;
UnionFind(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++)
parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]); // Path compression
return parent[x];
}
Interview Tips:
4. Algorithms
Sorting Algorithms
Bubble Sort
● Concept: Repeatedly swap adjacent elements if they are in the wrong order.
● Time Complexity: O(n²)
● Space Complexity: O(1)
● Stability: Stable
Implementation:
Selection Sort
● Concept: Repeatedly select the minimum element from the unsorted portion and move it
to the sorted portion.
● Time Complexity: O(n²)
● Space Complexity: O(1)
● Stability: Not stable (can be made stable with modifications)
Implementation:
Insertion Sort
● Concept: Build the sorted array one element at a time by comparing and inserting
elements into their correct position.
● Time Complexity: O(n²)
● Space Complexity: O(1)
● Stability: Stable
Implementation:
Merge Sort
● Concept: Divide the array into halves, recursively sort each half, and merge the sorted
halves.
● Time Complexity: O(n log n)
● Space Complexity: O(n)
● Stability: Stable
Implementation:
// Temporary arrays
int[] L = new int[n1];
int[] R = new int[n2];
Quick Sort
● Concept: Pick a pivot element, partition the array around the pivot, and recursively sort
partitions.
● Time Complexity:
○ Average Case: O(n log n)
○ Worst Case: O(n²) (when the smallest or largest element is always chosen as
pivot)
● Space Complexity: O(log n) (due to recursive calls)
● Stability: Not stable
Implementation:
When to Use: Efficient for large datasets; however, care must be taken to avoid worst-case
scenarios.
Heap Sort
● Concept: Build a heap from the data, then repeatedly extract the maximum element
from the heap and reconstruct the heap.
● Time Complexity: O(n log n)
● Space Complexity: O(1)
● Stability: Not stable
Implementation:
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
maxHeapify(arr, n, largest);
}
}
Counting Sort
Limitations:
Radix Sort
● Concept: Processes each digit of the numbers, starting from the least significant digit to
the most significant digit.
● Time Complexity: O(d*(n + k)), where d is the number of digits.
● Space Complexity: O(n + k)
● Stability: Stable
When to Use: Efficient for sorting numbers with fixed digit length.
Searching Algorithms
Linear Search
● Concept: Sequentially checks each element until a match is found or the list is
exhausted.
● Time Complexity: O(n)
Implementation:
Binary Search
● Concept: Repeatedly divides the sorted array in half to locate the target value.
● Time Complexity: O(log n)
● Prerequisite: The array must be sorted.
Iterative Implementation:
Interview Tips:
Recursion
Key Components:
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
Tail Recursion:
Backtracking
Applications:
void solveNQueens(int n) {
int[] board = new int[n];
placeQueen(board, 0, n);
}
Interview Tips:
Definition:
● A method for solving complex problems by breaking them down into simpler
subproblems.
● It is applicable when the problem has overlapping subproblems and optimal
substructure.
Approaches:
Memoization (Top-Down): Store the results of expensive function calls and return the cached
result when the same inputs occur again.
Tabulation (Bottom-Up): Build up a table in a bottom-up manner and use it to solve the
problem.
java
Copy code
int fib(int n) {
if (n <= 1) return n;
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
2.
Common DP Problems:
● Knapsack Problem
● Longest Common Subsequence (LCS)
● Coin Change Problem
● Edit Distance
Interview Tips:
Greedy Algorithms
Definition:
● An algorithmic paradigm that builds up a solution piece by piece, always choosing the
next piece that offers the most immediate benefit.
Characteristics:
● Locally Optimal Choice: The choice that seems the best at the moment.
● Global Optimal Solution: Not always guaranteed, but works for problems with the
greedy-choice property.
Interview Tips:
Definition:
Applications:
● Merge Sort
● Quick Sort
● Binary Search
● Karatsuba Algorithm for Multiplication
Interview Tips:
1. Dijkstra's Algorithm:
○ Purpose: Find the shortest path from a single source to all other vertices in a
graph with non-negative edge weights.
○ Time Complexity: O((V + E) log V) using a min-priority queue.
2. Bellman-Ford Algorithm:
○ Purpose: Handles graphs with negative edge weights (but no negative cycles).
○ Time Complexity: O(V * E)
3. Floyd-Warshall Algorithm:
○ Purpose: Find shortest paths between all pairs of vertices.
○ Time Complexity: O(V³)
Interview Tips:
1. Prim's Algorithm:
○ Approach: Builds the MST by adding the cheapest edge from the tree to a
vertex not yet in the tree.
○ Time Complexity: O(E log V)
2. Kruskal's Algorithm:
○ Approach: Builds the MST by adding the smallest edges while avoiding cycles.
○ Time Complexity: O(E log E)
Interview Tips:
Topological Sorting
● Definition: Linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for
every directed edge UV, vertex U comes before V.
● Algorithms:
○ DFS-based method.
○ Kahn's algorithm (uses BFS).
Interview Tips:
String Algorithms
Pattern Matching
1. Naïve Approach:
○ Check for the pattern at every possible position in the text.
○ Time Complexity: O(n * m)
2. Knuth-Morris-Pratt (KMP) Algorithm:
○ Purpose: Efficiently search for a pattern in a text.
○ Time Complexity: O(n + m)
○ Concept: Preprocess the pattern to create a longest proper prefix which is also
suffix (LPS) array.
● Suffix Tree:
○ A compressed trie of all the suffixes of a given string.
○ Applications:
■ Pattern matching.
■ Longest common substring.
● Suffix Array:
○ An array of starting indices of suffixes of a string sorted lexicographically.
○ Advantages over Suffix Trees:
■ Requires less memory.
■ Easier to implement.
5. Advanced Topics
Complexity Classes
Interview Tips:
Parallel Algorithms
Interview Tips:
9. Additional Resources
● Books:
○ "Introduction to Algorithms" by Cormen et al.
○ "Algorithms" by Robert Sedgewick and Kevin Wayne
● Online Platforms:
○ LeetCode: Extensive problem sets for practice.
○ HackerRank: Practice coding and compete.
○ GeeksforGeeks: Tutorials and practice problems.
● Tutorials:
○ Coursera: Algorithms courses by top universities.
○ MIT OpenCourseWare: Free lecture notes and videos.
Final Advice: