Files
codetutor/backend/data/patterns/patterns.yaml

260 lines
8.6 KiB
YAML

patterns:
- name: Two Pointers
slug: two-pointers
description: Use two pointers to traverse data from different positions, often moving toward or away from each other.
when_to_use: |
- Sorted arrays where you need to find pairs
- Linked list cycle detection
- Removing duplicates in-place
- Partitioning arrays
- Palindrome checking
- name: Sliding Window
slug: sliding-window
description: Maintain a window of elements that slides through the data, tracking a constraint or computing aggregates.
when_to_use: |
- Finding subarrays/substrings with specific properties
- Maximum/minimum sum of fixed-size windows
- Longest substring with at most K distinct characters
- Problems mentioning "contiguous" elements
- name: Fast & Slow Pointers
slug: fast-slow-pointers
description: Two pointers moving at different speeds, commonly used for cycle detection.
when_to_use: |
- Detecting cycles in linked lists or arrays
- Finding the middle of a linked list
- Finding the start of a cycle
- Happy number problem
- name: Binary Search
slug: binary-search
description: Efficiently search sorted data by repeatedly dividing the search space in half.
when_to_use: |
- Sorted arrays or search spaces
- Finding boundaries (first/last occurrence)
- Searching in rotated sorted arrays
- Finding peak elements
- Minimizing/maximizing with monotonic constraints
- name: BFS (Breadth-First Search)
slug: bfs
description: Level-by-level traversal using a queue, optimal for shortest paths in unweighted graphs.
when_to_use: |
- Shortest path in unweighted graphs
- Level-order tree traversal
- Finding all nodes at distance K
- Word ladder problems
- Matrix traversal
- name: DFS (Depth-First Search)
slug: dfs
description: Explore as deep as possible before backtracking, using recursion or a stack.
when_to_use: |
- Tree/graph traversal
- Path finding
- Detecting cycles
- Topological sorting
- Connected components
- Backtracking problems
- name: Backtracking
slug: backtracking
description: Build solutions incrementally, abandoning paths that fail to satisfy constraints.
when_to_use: |
- Generating all permutations/combinations
- Sudoku solver
- N-Queens problem
- Subset/partition problems
- Word search in grid
- name: Dynamic Programming
slug: dynamic-programming
description: Break problems into overlapping subproblems, storing results to avoid recomputation.
when_to_use: |
- Optimization problems (min/max)
- Counting problems
- Problems with optimal substructure
- Sequence alignment
- Knapsack-type problems
- name: Greedy
slug: greedy
description: Make locally optimal choices at each step, hoping for a global optimum.
when_to_use: |
- Interval scheduling
- Huffman coding
- Minimum spanning tree
- Activity selection
- When local optimum leads to global optimum
- name: Monotonic Stack
slug: monotonic-stack
description: Stack maintaining elements in sorted order, used for next greater/smaller element problems.
when_to_use: |
- Next greater/smaller element
- Largest rectangle in histogram
- Daily temperatures
- Stock span problem
- Trapping rain water
- name: Heap / Priority Queue
slug: heap
description: Data structure for efficiently finding min/max elements.
when_to_use: |
- Top K elements
- K-way merge
- Median from data stream
- Task scheduling
- Dijkstra's algorithm
- name: Union Find
slug: union-find
description: Track disjoint sets with efficient union and find operations.
when_to_use: |
- Connected components in graphs
- Cycle detection in undirected graphs
- Kruskal's minimum spanning tree
- Social network connections
- Accounts merge
- name: Trie
slug: trie
description: Tree-like data structure for efficient string prefix operations.
when_to_use: |
- Autocomplete systems
- Word dictionary with wildcard search
- Longest common prefix
- Word break problems
- Spell checker
- name: Prefix Sum
slug: prefix-sum
description: Precompute cumulative sums to answer range queries in O(1) time.
when_to_use: |
- Range sum queries
- Subarray sum equals target
- Product of array except self
- Count subarrays with given sum
- 2D matrix region sums
- name: LinkedList In-Place Reversal
slug: linkedlist-reversal
description: Reverse linked list nodes in-place by manipulating pointers without extra space.
when_to_use: |
- Reverse entire linked list
- Reverse a portion of linked list
- Reverse in groups of K
- Palindrome linked list check
- Reorder list problems
- name: Overlapping Intervals
slug: intervals
description: Process and merge intervals that share common ranges.
when_to_use: |
- Merge overlapping intervals
- Insert interval into sorted list
- Meeting rooms scheduling
- Find gaps between intervals
- Interval intersection problems
- name: Binary Tree Traversal
slug: tree-traversal
description: Visit tree nodes in specific orders - preorder, inorder, postorder, or level-order.
when_to_use: |
- Serialize/deserialize trees
- Validate BST (inorder)
- Copy or compare trees
- Path sum problems
- Tree construction from traversals
- name: Matrix Traversal
slug: matrix-traversal
description: Navigate 2D grids using DFS, BFS, or directional iteration.
when_to_use: |
- Number of islands / connected regions
- Shortest path in grid
- Flood fill
- Rotting oranges
- Word search in grid
- name: Topological Sort
slug: topological-sort
description: Order vertices in a directed acyclic graph (DAG) such that for every edge u→v, u comes before v.
when_to_use: |
- Course scheduling with prerequisites
- Build order / dependency resolution
- Task scheduling with dependencies
- Alien dictionary problems
- Detecting cycles in directed graphs
- name: Bit Manipulation
slug: bit-manipulation
description: Use bitwise operations (AND, OR, XOR, shifts) to solve problems efficiently.
when_to_use: |
- Finding single/missing numbers (XOR tricks)
- Counting set bits
- Power of two checks
- Subset generation with bitmasks
- Swapping without temp variable
- name: Counting / Bucket Sort
slug: counting-sort
description: Sort or process elements by using their values as indices when the range is bounded.
when_to_use: |
- Top K frequent elements (bounded frequency range)
- Sort Colors (Dutch National Flag)
- H-Index calculation
- Problems where values are in known small range
- Frequency-based grouping
- name: Cyclic Sort
slug: cyclic-sort
description: Place each element at its correct index when values are in range [1, n] or [0, n-1].
when_to_use: |
- Find missing number in [1, n]
- Find duplicate in [1, n]
- Find all missing/duplicate numbers
- First missing positive
- Set mismatch problems
- name: Divide and Conquer
slug: divide-and-conquer
description: Split problem into smaller subproblems, solve recursively, and combine results.
when_to_use: |
- Merge sort and related problems
- Quick select for kth element
- Binary search variants
- Matrix multiplication
- Closest pair of points
- name: Hashing
slug: hashing
description: Use hash tables for O(1) average lookup, insertion, and deletion.
when_to_use: |
- Two Sum and variants
- Detecting duplicates
- Frequency counting
- Anagram grouping
- LRU/LFU cache implementation
- name: Matrix Manipulation
slug: matrix-manipulation
description: Transform or process 2D matrices using rotation, transposition, or in-place modifications.
when_to_use: |
- Rotate image 90 degrees
- Spiral matrix traversal
- Set matrix zeroes
- Transpose matrix
- Game of Life state updates
- name: Synchronization
slug: synchronization
description: Coordinate multiple threads or processes using locks, semaphores, or barriers.
when_to_use: |
- Print in order problems
- Producer-consumer patterns
- Dining philosophers
- Building H2O molecules
- Concurrent data structure design