Coding interview patterns are reusable algorithmic blueprints that map large families of problems to a small set of well-understood techniques. Mastering these patterns matters because interviewers at top companies deliberately recycle the same underlying structures β recognizing the pattern instantly is the difference between a clean O(n) solution and a frustrated brute-force attempt. The key insight is that pattern recognition, not memorization, is the skill being tested: the same two-pointer template that solves "pair sum in sorted array" also solves "container with most water" and "3Sum". Each pattern below includes the canonical template, the signal that tells you to apply it, and the complexity you should expect to quote.
What This Cheat Sheet Covers
This topic spans 15 focused tables and 96 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Two Pointers
Two pointers place one pointer at each end of a sorted (or partitioned) array and move them toward each other based on a condition, eliminating brute-force O(nΒ²) pair enumeration. A second variant uses both pointers moving in the same direction for partitioning or fast/slow detection β that variant is covered separately in Table 3.
| Technique | Example | Description |
|---|---|---|
l, r = 0, len(arr)-1while l < r: if arr[l]+arr[r]==target: return [l,r] elif arr[l]+arr[r]<target: l+=1 else: r-=1 | β’ Works on sorted arrays β’ shrink the window from both ends based on whether the sum is too small or too large | |
nums.sort()for i in range(len(nums)-2): l, r = i+1, len(nums)-1 # two-pointer inner loop | β’ Fix one element, then run opposite-direction two pointers on the remainder β’ reduces O(nΒ³) β O(nΒ²). | |
slow = 0for fast in range(1, len(nums)): if nums[fast] != nums[slow]: slow += 1 nums[slow] = nums[fast] | β’ slow marks the boundary of the valid prefixβ’ fast scans ahead β classic same-direction two pointers for in-place array compaction |