Coding Interview Patterns

Recognize the pattern, and the solution follows.

Most interview problems are variations on a small set of patterns. The skill that matters is recognition: spotting, from the wording and constraints, which pattern applies. Below is the core set Leet Study is organized around, with the signal that should trigger each and its typical complexity.

Hashing & counting

Trade memory for time: one pass builds a hash map of value → count or index, turning a nested rescan into O(1) lookups.

Signal: "how many times…", "first unique…", "find a pair that sums to…", or grouping by a key. Complexity: usually O(n) time, O(n) space.

Two pointers

Two indices walking toward each other (or in tandem) examine each element once, replacing the O(n²) "try every pair" loop.

Signal: a sorted array plus "find a pair/triplet", palindrome checks, or "do it in place with O(1) extra space". Complexity: O(n) after sorting.

Sliding window

Grow the right edge; shrink the left only when the window breaks an invariant. Every element enters and leaves at most once.

Signal: "longest/shortest substring or subarray such that…", a constraint on what the window may contain (≤ k distinct, no repeats). Complexity: O(n).

Binary search

Halve the search space each step. Beyond sorted arrays, you can "binary-search the answer" when a value is monotonic with respect to feasibility.

Signal: sorted input, or "minimum/maximum value that satisfies a condition". Complexity: O(log n), or O(n log n) when each check is O(n).

Stack & monotonic stack

A stack tracks the most recent relevant element. A monotonic stack keeps elements in increasing/decreasing order to answer "next greater/smaller" queries in linear time.

Signal: matching brackets, "next greater element", spans, or undo/redo behavior. Complexity: O(n).

Linked lists & fast/slow pointers

Two pointers at different speeds detect cycles and find the middle in one pass without extra space.

Signal: "detect a cycle", "find the middle", reversing or reordering a list. Complexity: O(n) time, O(1) space.

Trees — DFS & BFS

DFS (recursion) bubbles information up from children; BFS processes level by level and finds shortest paths in unweighted graphs.

Signal: "depth/height", "path sum", "level order", or "shortest number of steps". Complexity: O(n) over the nodes/edges.

Graphs

Model entities as nodes and relationships as edges, then traverse with DFS/BFS. Grid problems are graphs in disguise (each cell is a node).

Signal: connected components, islands in a grid, reachability, shortest path. Complexity: O(V + E).

Backtracking

Build a candidate incrementally and abandon ("backtrack") as soon as it can't lead to a valid solution. The workhorse for subsets, permutations and combinations.

Signal: "generate all…", "find all combinations/permutations", constraint puzzles. Complexity: exponential, but pruning keeps it tractable.

Dynamic programming

Break a problem into overlapping subproblems and reuse their answers. Identify the state, the transition, and the base case.

Signal: "number of ways", "min/max cost", "can you reach…", or an optimal-substructure phrasing. Complexity: often O(n) to O(n·m) depending on the state.

Heaps & top-K

A heap keeps the largest/smallest elements accessible in O(log n), ideal for "top K" and merging streams.

Signal: "k largest/smallest", "k most frequent", merging sorted sources. Complexity: O(n log k).

Ready to practice these? Pick a track on the Leet Study home page, and read the full interview-prep guide for a study plan.

← Back to Leet Study