Patterns

All patterns

22 patterns · 79 tagged problems

Sorted by problem count — most common first

Two Pointers

Junior10

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

Sliding Window

Junior8

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

0/1 Knapsack (DP)

Mid6

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

Tree DFS

Junior5

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

Fast & Slow Pointers (Floyd's)

Mid4

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

Backtracking

Senior4

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

Merge Intervals

Mid3

Sort intervals by start, then sweep merging overlaps.

Cyclic Sort

Mid3

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

Mid3

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

Tree BFS

Junior3

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

Subsets (BFS-style generation)

Mid3

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

Modified Binary Search

Mid3

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

Top K Elements

Mid3

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

Topological Sort

Senior3

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

Graph BFS/DFS

Junior3

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

Monotonic Stack/Queue

Mid3

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

Two Heaps

Senior2

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

Bitwise XOR

Mid2

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

K-Way Merge

Senior2

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

Trie (Prefix Tree)

Senior2

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

Union-Find (DSU)

Senior2

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

Greedy

Mid2

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