PatternPilot

Patterns Directory

Browse all 27 coding patterns with complexity and examples

Filter by difficulty:

Showing 27 of 27 patterns

Two Pointers
Pointer/Traversal
Use two pointers moving through data (often from opposite ends) to solve problems efficiently without extra space.
Time:
O(n)
Space:
O(1)
Easy
Fast & Slow Pointers
Pointer/Traversal
Use pointers moving at different speeds (typically 1x and 2x) to detect cycles or find specific positions.
Time:
O(n)
Space:
O(1)
Easy
Sliding Window
Array/String
Maintain a window of elements that satisfies certain conditions, expanding and contracting as needed.
Time:
O(n)
Space:
O(k)
Medium
Merge Intervals
Interval/Range
Sort intervals by start time and merge overlapping ones by tracking the maximum end time.
Time:
O(n log n)
Space:
O(n)
Medium
In-Place Linked List Manipulation
Linked List
Modify linked list structure without extra data structures by manipulating pointers.
Time:
O(n)
Space:
O(1)
Medium
Heaps / Priority Queues
Data Structure
Use min-heap or max-heap to efficiently track and access minimum or maximum elements.
Time:
O(log n) per operation
Space:
O(k)
Medium
K-way Merge
Merge/Heap
Merge K sorted lists efficiently using a min-heap of pointers to track the smallest current element.
Time:
O(n log k)
Space:
O(k)
Hard
Top K Elements
Heap
Find K largest or smallest elements using partial sorting via heap, avoiding full sort.
Time:
O(n log k)
Space:
O(k)
Medium
Binary Search
Search
Eliminate half of search space each iteration on sorted data to find target efficiently.
Time:
O(log n)
Space:
O(1)
Easy
Modified Binary Search
Search
Adapt binary search for rotated arrays, special conditions, or finding peaks and boundaries.
Time:
O(log n)
Space:
O(1)
Medium
Subsets
Recursion/Backtracking
Generate all combinations using include/exclude decisions or BFS approach.
Time:
O(2^n)
Space:
O(n)
Medium
Backtracking
Recursion
Explore all possibilities, pruning invalid branches early to avoid unnecessary exploration.
Time:
O(2^n)
Space:
O(n)
Hard
Greedy Algorithms
Algorithm
Make locally optimal choices at each step that lead to global optimum (with proof).
Time:
O(n log n)
Space:
O(1)
Medium
Dynamic Programming
Algorithm
Solve overlapping subproblems by storing results and combining them (memoization or tabulation).
Time:
O(n^2) or O(n*m)
Space:
O(n^2)
Hard
Cyclic Sort
Array
Place each number at its correct index in range 1-n to find missing/duplicate in O(n) time.
Time:
O(n)
Space:
O(1)
Easy
Topological Sort
Graph
Order vertices in DAG based on dependencies using DFS or Kahn's algorithm (remove 0-indegree).
Time:
O(V+E)
Space:
O(V)
Medium
Trie / Prefix Tree
Data Structure
Tree-based structure for storing strings, enabling efficient prefix-based searches and autocomplete.
Time:
O(m) per operation
Space:
O(ALPHABET_SIZE * N)
Medium
Hash Maps / Frequency Counter
Data Structure
Store key-value pairs with O(1) average lookup to count frequencies and track elements.
Time:
O(n)
Space:
O(n)
Easy
Union Find / Disjoint Set
Data Structure
Track connected components and merge sets efficiently with path compression and union by rank.
Time:
O(α(n)) amortized
Space:
O(n)
Medium
Bitwise Manipulation
Math
Operate on individual bits using AND, OR, XOR, NOT for space-efficient solutions.
Time:
O(n)
Space:
O(1)
Medium
Math & Geometry
Math
Apply mathematical formulas, number theory, and coordinate geometry for problem-solving.
Time:
Varies
Space:
Varies
Medium
Tree DFS (Depth-First Search)
Tree/Graph
Recursively traverse tree going deep on one branch before backtracking (inorder/preorder/postorder).
Time:
O(n)
Space:
O(h)
Medium
Tree BFS (Breadth-First Search)
Tree/Graph
Process tree level-by-level using queue, useful for shortest paths and level-order processing.
Time:
O(n)
Space:
O(w)
Easy
Graph DFS / BFS Traversal
Graph
General graph traversal using DFS (recursion/stack) or BFS (queue) to explore all nodes.
Time:
O(V+E)
Space:
O(V)
Medium
Matrices / 2D Arrays
2D Array
Operate on 2D grids including rotations, spirals, and multi-directional traversals.
Time:
O(m*n)
Space:
O(1) to O(m*n)
Medium
Stacks / Monotonic Stack
Data Structure
LIFO structure for managing call stacks, matching parentheses, or maintaining monotonic order.
Time:
O(n)
Space:
O(n)
Medium
Custom Data Structures
Design
Combine existing structures (hash map, linked list, heap) to optimize for specific operations.
Time:
O(1) typical
Space:
O(n)
Hard