Patterns

All patterns

22 patterns · 79 tagged problems

Sorted by problem count — most common first

Two Pointers

10

Two indices walk a sorted (or paired) structure from different positions to converge on a target.

Sliding Window

8

Maintain a window of elements over a sequence; expand/shrink to satisfy a constraint while tracking an aggregate.

0/1 Knapsack (DP)

6

Choose/skip decisions over items with capacity; tabulate optimal value for subproblems.

Tree DFS

5

Recursive or stack-based depth-first traversal; pre/in/post order.

Fast & Slow Pointers (Floyd's)

4

Two pointers move at different speeds through a linked structure to detect cycles or find midpoints.

Backtracking

4

DFS through a decision tree, undoing choices on failure to try alternatives.

Merge Intervals

3

Sort intervals by start, then sweep merging overlaps.

Cyclic Sort

3

Place each number at its index by swapping; O(n) sort when values are a known bounded range like 1..n.

In-Place Reversal of LinkedList

3

Reverse pointers segment by segment without extra space using prev/curr/next bookkeeping.

Tree BFS

3

Level-order traversal using a queue; emit one level at a time.

Subsets (BFS-style generation)

3

Iteratively or recursively build all subsets/permutations/combinations by extending prior results.

Modified Binary Search

3

Binary search on a non-trivial space: rotated arrays, infinite arrays, the answer itself.

Top K Elements

3

Maintain a heap of size k to track the k largest/smallest/most-frequent elements.

Topological Sort

3

Order a DAG so every edge points forward; Kahn's BFS using in-degrees, or DFS post-order.

Graph BFS/DFS

3

Explore a graph by levels (BFS) or branches (DFS), tracking visited nodes.

Monotonic Stack/Queue

3

Stack/deque maintained in increasing or decreasing order to answer next-greater/smaller queries.

Two Heaps

2

Maintain a max-heap for the lower half and a min-heap for the upper half of a stream.

Bitwise XOR

2

Use XOR's self-cancellation property to find unique elements or toggle bits efficiently.

K-Way Merge

2

Use a min-heap of k pointers (one per list) to merge k sorted sources in O(N log k).

Trie (Prefix Tree)

2

Tree where each edge is a character; O(L) insert/search/prefix.

Union-Find (DSU)

2

Disjoint-set structure with path compression + union by rank for near-O(1) component queries.

Greedy

2

Make a locally optimal choice at each step; prove (or trust) it yields a global optimum.