Sorting and searching questions are usually about structure. Sorting creates order; binary search exploits order; interval sweeps preserve a compact active state.
What To Recognize
| Problem Shape | Pattern | Practice |
|---|---|---|
| Only 0, 1, 2 values | Three pointers | Sort Colors |
| Next lexicographic order | Pivot and suffix reverse | Next Permutation |
| First/last target index | Boundary binary search | First and Last Position |
| Sorted matrix | Flattened binary search | Search a 2D Matrix |
Boundary Rule
When binary search feels buggy, write the meaning of your boundary first: first index greater than or equal to target, first index greater than target, or last index less than target.
FAQs
Is sorting allowed in interviews?
Yes, but explain the cost and whether sorting changes any required original order.
Why do binary search bugs happen?
Most bugs come from not defining whether the search interval is closed or half-open.