Patterns are reusable problem-solving idioms — not full named algorithms like Dijkstra, but recurring techniques you apply to turn a brute-force solution into an efficient one. Recognising the pattern is usually the hard part of a coding problem; once you see “this is a sliding window,” the implementation follows. They differ from paradigms (DP, greedy, backtracking), which are broader design philosophies — patterns are the concrete moves.

Algorithm Selection

PatternThe moveTells you to reach for itTypical win
Two PointersTwo coordinated indices, ends-inSorted array, pair/triplet sums, in-place partitionO(n²) → O(n)
Fast and Slow PointersTwo indices at different speeds”Cycle in a linked list”, “find the middle in one pass”, duplicate in 1..nO(n) space → O(1)
Sliding WindowA moving contiguous range updated incrementally”Longest/shortest contiguous subarray or substring with a constraint”O(n·k) → O(n)
Prefix SumPrecompute cumulative sums; a range is one subtraction”Many range-sum queries over static data”, “count subarrays summing to k”O(n) per query → O(1)
Monotonic Stack and QueueA stack or deque kept sorted, popping what can never win”Next/previous greater element”, “sliding-window maximum”O(n²) → O(n)
Merge IntervalsSort by start, then sweep and coalesce”Overlapping intervals”, “meeting rooms”, calendar bookingO(n²) → O(n log n)
Cyclic SortSwap each value to the index it belongs at”n numbers in the range 1..n” + find the missing/duplicate, in placeO(n) space → O(1)
Top-K ElementsA size-k heap over a stream”Top / largest / smallest / most frequent K”O(n log n) → O(n log k)
Binary Search on AnswerBinary-search the answer space, not the array”Minimise the maximum”, “maximise the minimum”, “smallest x such that…”O(range) → O(log range)
Bit ManipulationOperate on the binary representation directlySmall fixed sets, parity/toggles, subset enumerationO(n) → O(1) space/time tricks

TIP

The interview skill is recognition: most problems announce their pattern through a keyword — “contiguous … with sum/length” → sliding window; “sorted … pair that sums to” → two pointers; “next greater” → monotonic stack; “minimise the maximum” → binary search on answer; “appears once / subsets of a small set” → bit manipulation.

A few of these are close relatives, and the distinction is worth holding onto. Sliding Window needs all-positive values to keep its monotonic shrink valid; Prefix Sum plus a hashmap handles the same “subarray summing to k” question when negatives are allowed. Fast and Slow Pointers is the speed-differential member of the Two Pointers family rather than a separate idea. And Binary Search on Answer is not really a search over data at all — it borrows only the halving mechanic from Binary Search.

References

10 items under this folder.