Algorithms and Data Structures Cheatsheet

5441
Jun 12, 2024

Algorithms and Data Structures Cheatsheet

1. Arrays and Strings

Arrays

  • Definition: Fixed-size, indexed collection of elements.
  • Common Operations: Traversal, Insertion, Deletion, Searching.
  • Techniques: Two-pointer approach, Sliding window.

Strings

  • Definition: Sequence of characters.
  • Common Operations: Concatenation, Substring, Palindrome check.
  • Techniques: Two-pointer approach, KMP algorithm for pattern matching.

2. Linked Lists

Singly Linked List

  • Definition: Each node contains data and a reference to the next node.
  • Common Operations: Traversal, Insertion, Deletion, Reversal.
  • Techniques: Fast and slow pointers (detecting cycles), Merging lists.

Doubly Linked List

  • Definition: Each node contains data and references to both the next and previous nodes.
  • Common Operations: Traversal, Insertion, Deletion.

3. Stacks and Queues

Stack

  • Definition: LIFO (Last In First Out) structure.
  • Common Operations: Push, Pop, Peek.
  • Applications: Expression evaluation, Backtracking, Function call stack.

Queue

  • Definition: FIFO (First In First Out) structure.
  • Common Operations: Enqueue, Dequeue, Front.
  • Applications: Scheduling, Breadth-first search (BFS).

4. Trees

Binary Tree

  • Definition: Each node has at most two children (left and right).
  • Common Operations: Traversal (In-order, Pre-order, Post-order), Insertion, Deletion.
  • Techniques: Recursion, Iterative traversal using stack.

Binary Search Tree (BST)

  • Definition: A binary tree with ordered nodes.
  • Common Operations: Search, Insert, Delete, In-order traversal (yields sorted order).

Heap

  • Definition: A complete binary tree used to implement priority queues.
  • Types: Min-heap (parent <= children), Max-heap (parent >= children).
  • Common Operations: Insert, Extract-Min/Max, Heapify.

5. Graphs

Graph

  • Definition: Collection of nodes (vertices) and edges.
  • Types: Directed, Undirected, Weighted, Unweighted.
  • Representations: Adjacency matrix, Adjacency list.

Graph Traversal

  • Breadth-First Search (BFS): Uses a queue, explores neighbors level by level.
  • Depth-First Search (DFS): Uses a stack (or recursion), explores as far as possible along each branch before backtracking.

Graph Algorithms

  • Shortest Path: Dijkstra's algorithm, Bellman-Ford algorithm.
  • Minimum Spanning Tree: Kruskal's algorithm, Prim's algorithm.
  • Topological Sorting: Kahn’s algorithm, DFS-based approach.

6. Dynamic Programming (DP)

Key Concepts

  • Memoization: Top-down approach, caching results of subproblems.
  • Tabulation: Bottom-up approach, building up solutions from base cases.

Common Problems

  • Fibonacci Sequence: ( F(n) = F(n-1) + F(n-2) )
  • Knapsack Problem: Maximizing value with weight constraints.
  • Longest Common Subsequence (LCS): Finding the longest subsequence present in both sequences.

7. Sorting Algorithms

Bubble Sort

  • Description: Repeatedly swap adjacent elements if they are in the wrong order.

Selection Sort

  • Description: Select the minimum element and move it to the sorted part.

Insertion Sort

  • Description: Build the sorted array one element at a time by inserting elements into their correct position.

Merge Sort

  • Description: Divide-and-conquer algorithm, recursively split and merge arrays.

Quick Sort

  • Description: Divide-and-conquer algorithm, partition the array around a pivot.

8. Searching Algorithms

  • Description: Sequentially checks each element until the target is found.
  • Description: Divide and conquer, works on sorted arrays, repeatedly divide the search interval in half.

9. Hashing

Hash Table

  • Description: Data structure that maps keys to values for efficient lookup.
  • Common Operations: Insert, Delete, Search.
  • Collision Resolution: Chaining, Open addressing.

10. Bit Manipulation

Common Operations

  • AND, OR, XOR, NOT: Basic bit operations.
  • Shift left, Shift right: Bit shifting operations.

Techniques

  • Checking if a number is odd/even.
  • Swapping values.
  • Counting set bits.

11. Recursion

Concept

  • Definition: A function calling itself.
  • Base Case: Condition under which the recursion stops.
  • Recursive Case: The part of the function where it calls itself.

Common Problems

  • Factorial
  • Fibonacci
  • Tower of Hanoi
  • Permutations
Comments (1)