Why this lesson has no visualizer
This lesson is about choosing between techniques, not about one algorithm, so there is no single trace worth stepping through. Every technique it mentions has its own lab: follow the links to the pattern pages and lessons, where each one can be run on your own input.
Step 1: read the size limit first
The largest input size tells you which growth rates can finish. A rough budget is on the order of 10⁸ simple operations per second in C++, somewhat less in C# and Java, and noticeably less in CPython. Treat these as orders of magnitude, not promises: the constant factors of your code and the machine matter.
| Largest n | Growth rates that usually fit | Typical techniques |
|---|---|---|
| ≤ 10–12 | O(n!), O(n · 2ⁿ) | backtracking, brute force over permutations |
| ≤ 20–25 | O(2ⁿ), O(n² · 2ⁿ) | subsets, bitmask DP |
| ≤ 500 | O(n³) | triple loops, interval DP |
| ≤ 5 000 | O(n²) | pairwise comparison, 2D DP tables |
| ≤ 10⁶ | O(n log n), O(n) | sorting, heaps, two pointers, sliding window, prefix sums, hashing |
| values up to 10⁹ or more | O(log V) over the answer | binary search on the answer |
The patterns cheatsheet has the same table with more signals, and the Big O explorer shows how fast each curve grows.
Value ranges matter as much as n. A sum of 10⁶ values up to 10⁹ needs 64-bit integers in C#, Java and C++; Python's integers never overflow but get slower as they grow. The language gotchas cheatsheet lists the traps.
Step 2: look for signals in the wording
| Signal in the problem | Technique to try first |
|---|---|
| sorted input, or you may sort | two pointers, binary search |
| contiguous subarray or substring, "longest/shortest with a condition" | sliding window (needs a condition that stays true as the window shrinks) |
| many range-sum queries on a fixed array | prefix sums |
| "have we seen this before?", counting, grouping | hashing |
| "k-th largest", "top k", repeated minimum | heap |
| next greater or smaller element | monotonic stack |
| fewest steps in an unweighted grid or graph | BFS |
| non-negative weights, cheapest path | Dijkstra |
| "must happen before", prerequisites | topological sort |
| merging groups, "are these connected?" over time | union-find |
| overlapping intervals, meetings, bookings | sort, then sweep |
| "smallest value that works", monotonic yes/no | binary search on the answer |
| "number of ways", "best total" with overlapping choices | dynamic programming |
| "all combinations", small n | backtracking |
A signal is a hint, not a proof. Sliding window, for example, only works when extending the window can only make the condition worse (or only better); with negative numbers in a "sum at most k" question it breaks, which the sliding window lesson shows with a counterexample.
Step 3: combine when one technique is not enough
Harder problems usually need two ideas, each removing one factor from the cost:
- Sort + two pointers. Sorting costs O(n log n) once and turns an O(n²) pair search into O(n).
- Hashing + prefix sums. Count subarrays with sum k by storing how often each prefix sum has appeared: O(n) instead of O(n²). See subarrays that sum to k.
- Sort + heap. Sort meetings by start time and keep a min-heap of end times to find how many rooms are needed at once (worked through below).
- Hash map + heap. Count frequencies, then keep the k most frequent in a heap of size k: the k values seen most often.
- Binary search + greedy check. Guess an answer, check it greedily in O(n), and halve the range: the search on the answer lesson.
When you combine, say what each part contributes. "Sorting gives order, the heap gives the current minimum in O(log n)" is a sentence an interviewer can check; "I used a heap" is not.
Step 4: argue correctness before trusting it
Pick the argument that fits the technique:
- Loop invariant for scans, two pointers and binary search: what is true before every iteration, why it stays true, and why it gives the answer at the end (loop invariants lesson).
- Exchange argument for greedy choices: any optimal solution can be changed step by step into the greedy one without getting worse (interval scheduling).
- Recurrence and base cases for recursion and dynamic programming: each state is computed from smaller states that are already correct.
If you cannot state the argument, test harder before trusting the solution: many plausible greedy rules are wrong.
Step 5: test like you mean it
- Edge cases: empty input, one element, all equal, already sorted, reverse sorted, the largest and smallest values, negative numbers if allowed.
- Brute-force cross-check: write the obvious slow solution and compare outputs on many small random inputs. Every algorithm in this lab is tested that way in all four languages; the problem pages show the brute force next to the approach.
- Worst case for time: build the input that makes your solution slowest (for example, all equal values for a Lomuto quicksort, see the quicksort lesson) and check it stays within the limit.
A worked example
Given up to 10⁵ meeting intervals with times up to 10⁹, how many rooms are needed so that no two meetings in the same room overlap?
- Size: n = 10⁵ rules out O(n²) pairwise checks; O(n log n) fits easily.
- Values: times up to 10⁹ fit in 32 bits, but rule out an array indexed by time.
- Signal: overlapping intervals, so sort and sweep.
- Combination: sort by start; a min-heap holds the end times of meetings in progress. Before placing a meeting, pop every end time at or before its start. The heap size after each push is the number of rooms in use, and the answer is its maximum.
- Correctness: at the moment a meeting starts, the heap contains exactly the meetings still in progress, so its size is the number of rooms needed at that instant; the answer is the largest such number.
- Tests: no meetings (0 rooms), back-to-back meetings that touch (half-open intervals share a room), all meetings at the same time (n rooms).
The rooms needed problem page solves the same question with a second combination, sort + two pointers: sort the start times and the end times separately and walk both lists, since rooms are interchangeable. Both run in O(n log n); comparing them is a good exercise in seeing that one problem can admit several correct designs.
Summary
Read the limits, list the signals, choose and if needed combine techniques, argue correctness, then test with edge cases, a brute-force cross-check and the worst case. The interview learning paths apply this checklist to a sequence of solved problems.