🧩 Pattern Library

Master the patterns that matter. Each pattern includes pseudocode, complexity analysis, recognition signals, and linked practice problems.

Two Pointers

→
⏱ O(n)💾 O(1)8 problems0/8 solved

Use two pointers (usually `left` and `right`) to traverse an array from both ends toward the center, or both from the start at different speeds. Works best on sorted arrays or when you need to find pairs/triplets.

Array is sorted (or can be sorted)Finding pairs that satisfy a conditionComparing elements from both ends

Sliding Window

→
⏱ O(n)💾 O(1) to O(k)5 problems0/5 solved

Maintain a window (subarray) that slides across the array. Expand or shrink the window to find optimal subarrays that satisfy a condition. Avoids nested loops by reusing computation from the previous window position.

Finding max/min subarray of fixed sizeLongest/shortest substring with a conditionContiguous subarray problems

Binary Search

→
⏱ O(log n)💾 O(1)5 problems0/5 solved

Repeatedly divide the search space in half. Compare the middle element with the target — if smaller, search the right half; if larger, search the left half. Requires sorted data.

Searching in sorted arraysFinding boundaries (first/last occurrence)Optimization problems (minimize/maximize)

Depth First Search

→
⏱ O(N)💾 O(H)2 problems0/2 solved

Traverse deep into a graph or tree before backtracking. Often implemented using recursion or an explicit stack. Excellent for exploring all paths or finding specific structures like connected components.

Exploring all possible pathsTree traversal (pre/in/post order)Finding connected components

Hash Map

→
⏱ O(N)💾 O(N)1 problems0/1 solved

Use a hash map or dictionary to store key-value pairs for O(1) average time complexity lookups.

Fast lookups requiredCounting frequenciesMapping relationships

Stack

→
⏱ O(N)💾 O(N)1 problems0/1 solved

A Last-In-First-Out (LIFO) data structure. Useful for parsing, evaluating expressions, and tracking state that needs to be reversed.

Parsing expressionsMatching parenthesesReversing sequences

Greedy

→
⏱ O(N log N)💾 O(1)1 problems0/1 solved

Make the locally optimal choice at each stage with the hope of finding a global optimum. Often requires sorting the input first.

Optimization problemsInterval merging/schedulingLocal optimum leads to global optimum

Breadth-First Search

→
⏱ O(V + E)💾 O(V)1 problems0/1 solved

Explore a graph or tree level by level using a queue. Guarantees finding the shortest path on unweighted graphs.

Shortest path in unweighted graphsLevel-order traversalFinding nearest neighbors

Graph Traversal

→
⏱ O(V + E)💾 O(V)1 problems0/1 solved

Traverse and manipulate networks of vertices connected by edges. Common representations include adjacency lists and matrices, explored via Breadth-First or Depth-First search.

Network routing and topologySocial network connectionsDependency resolution and cycle detection

Trie (Prefix Tree)

→
⏱ O(L) per operation💾 O(N * L)1 problems0/1 solved

A tree-like data structure used to store a dynamic set or associative array where the keys are usually strings. Efficient for prefix-based searches and auto-completion.

Autocomplete and typeahead searchSpell checkersPrefix matching IP routing

Heap / Priority Queue

→
⏱ O(N log K)💾 O(K)1 problems0/1 solved

A specialized tree-based data structure that satisfies the heap property. In a min-heap, the root is the minimum element, enabling O(1) retrieval and O(log N) insertion/deletion.

Top K elementsMerge K sorted streamsTask scheduling with priorities

Dynamic Programming

→
⏱ O(N) to O(N * M)💾 O(N) to O(N * M)2 problems0/2 solved

Solve complex problems by breaking them down into simpler subproblems, storing results of subproblems to avoid redundant computations (optimal substructure and overlapping subproblems).

Optimization (minimize / maximize)Counting combinations or distinct pathsOverlapping subproblems and optimal substructure

Backtracking

→
⏱ O(2^N) or O(N!)💾 O(N)1 problems0/1 solved

An algorithmic technique that considers searching every possible combination to solve a computational problem. It builds candidates incrementally and abandons (backtracks) a candidate as soon as it cannot lead to a valid solution.

Generating all subsets, permutations, or combinationsConstraint satisfaction problems (Sudoku, N-Queens)Exhaustive search with pruning