DSA · Practice

Pattern Checklist

The handful of patterns behind most interview problems — what triggers each one, the shape of the solution, and what it costs.

— min read DSA

Recognise, Then Recall

Interview problems are not each unique. A few dozen patterns cover most of them, and the skill being tested is recognising which one applies — not inventing an algorithm on the spot.

The productive way to practise is therefore not "solve 500 problems" but "recognise 20 patterns and solve enough of each to know their edges". After that, an unseen problem is usually a familiar pattern wearing different words.

This page is a checklist to work down: the trigger that tells you a pattern applies, the shape of the solution, and its cost. Each links to the topic that teaches it properly.

Array & String Patterns

Trigger in the problemPatternCost
Sorted array, find a pair or a targetTwo pointers from both endsO(n)
Contiguous subarray of size k, or "longest substring with…"Sliding windowO(n)
Repeated range sumsPrefix sums computed onceO(n) build, O(1) query
"Have I seen this before"Hash map of seen valuesO(n)
Sorted input, find one valueBinary searchO(log n)
Next greater or smaller elementMonotonic stackO(n)
In-place rearrangementRead and write pointersO(n), O(1) space

Tree & Graph Patterns

Trigger in the problemPatternCost
Shortest path in an unweighted graphBFS with a queueO(V+E)
Explore every path, detect a cycleDFS with recursion or a stackO(V+E)
Weighted shortest path, no negative edgesDijkstra with a heapO(E log V)
Ordering with prerequisitesTopological sortO(V+E)
Sorted output from a BSTIn-order traversalO(n)
Level-by-level processingBFS tracking the queue size per levelO(n)
Prefix matching over many stringsTrieO(length)
Grids are graphs. "Islands", "rotting oranges" and "shortest path in a maze" are BFS or DFS with neighbours defined as up, down, left and right — recognising that converts a whole category into one you already know.

Optimisation Patterns

Trigger in the problemPatternCost
"Maximum", "minimum" or "count the ways", with overlapping subproblemsDynamic programmingUsually O(n·m)
A locally best choice is provably globally bestGreedyOften O(n log n) with a sort
Generate every combination or permutationBacktracking on a recursive treeExponential — expected
Top or bottom k elementsHeap of size kO(n log k)
Repeated merging of groupsUnion-findNear O(1) amortised

The dynamic-programming tell is worth memorising: overlapping subproblems plus optimal substructure. If a brute-force recursion recomputes the same state, memoise it — that step alone converts most exponential solutions into polynomial ones.

Greedy is the pattern people reach for wrongly. It needs an argument that the local choice is globally optimal; without one it produces a solution that passes the examples and fails on a case you did not think of.

A Method For The Interview

StepWhat to say and do
ClarifyInput size, ranges, duplicates, empty input, what to return on no answer
State a brute forceGive its complexity out loud — it shows you know what you are improving on
Name the pattern"This is a sliding window because we want the longest contiguous…"
Walk one exampleOn paper, before writing code — most bugs are caught here
Write itTalking through the invariant as you go
Test the edgesEmpty, single element, all duplicates, maximum size
State the complexityTime and space, and where the bound comes from
Say the brute force first, always. It gives you a working baseline, buys thinking time, and gives the interviewer something to nudge — going silent and hunting for the optimal answer is the most common way a strong candidate stalls.

When stuck: sort the input and see what becomes possible, try a hash map for the lookup, ask whether the input structure hints at binary search, or solve a smaller version and look for the recurrence. See Big-O Notation for stating the bound precisely.

Interview Questions

How do you decide between sliding window and two pointers?

Both walk an array once. Sliding window maintains a contiguous range with a running aggregate; two pointers converge from the ends, typically on sorted input, to find a pair or partition.

What tells you a problem is dynamic programming?

Overlapping subproblems and optimal substructure — a brute-force recursion that recomputes the same state. Memoising that recursion is usually the whole solution.

When is greedy safe?

Only when you can argue the locally optimal choice is globally optimal. Without that argument greedy passes the examples and fails on a case nobody tried.

How do you recognise a graph problem in disguise?

Grids, dependencies, word ladders and state machines are all graphs. If states connect to neighbouring states, BFS or DFS applies regardless of how the problem is phrased.

What is the first thing to do with a new problem?

Clarify the constraints, then state a brute force with its complexity. It gives a baseline, buys thinking time, and makes the optimisation conversation concrete.

How do you find the top k elements efficiently?

A heap of size k in O(n log k), rather than sorting everything in O(n log n) — and it works on a stream, where sorting does not.

Quick Quiz

1. "Longest substring without repeating characters" suggests…
2. Shortest path in an unweighted graph uses…
3. Overlapping subproblems and optimal substructure indicate…
4. Top k elements from a stream is best done with…
5. The first thing to state in an interview is…