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

180 lines
5.9 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