- Algorithms
- Search and sort
Search and sort
Techniques for finding things fast: swapping a nested loop for a map, sorting first or halving the search space.
3 guides35 min in totalAlgorithms
The guides, in order
Read them top to bottom: each one builds on the ones before. The ones you've read carry a check.
- Nested Loops vs Maps and Sets: Trading Memory for SpeedLearn to replace O(n²) nested loops with one pass over a hash map or set for complements, counting and grouping, and how to explain the memory cost.12 min readO(n) expected, O(n²) naiveStart here
- Sort to Simplify: Turn Messy Input into a Single PassLearn when sorting first reduces a problem to one linear scan, covering neighbours, interval sweeps, multi-key comparators and stability.11 min readO(n log n)
- Binary Search: First True, Answer Spaces and Edge CasesLearn binary search as a hunt for the first true value of a monotone predicate, including searching answer ranges and getting the boundaries right.12 min readO(n log R) for the example; O(log n) for plain array search
About this group
- What you'll find
- O(1) lookups with maps and sets, sorting to solve in one pass (neighbours, intervals, comparators) and binary search, including binary search on the answer.
- It's for you if…
- your first solution usually has two nested loops and you don't know how to make it faster.
- How each guide works
- Each guide starts with the intuition and the signs that tell you to use the technique, then the steps and its time and memory cost. It includes a step-by-step traced example, code in TypeScript, Python and PHP, variants, common mistakes and frequently asked questions.