The Plain Picture#
Before a trip, you check how far it is. 2 km — walk. 20 km — take a scooter. 2,000 km — you need a plane. The distance tells you which vehicle is even possible, before you plan the route.
The Real Name#
Every competitive-programming problem and most interview problems state constraints like 1 ≤ n ≤ 10⁵. Combined with a rough speed of
about 10⁸ simple operations per second, the constraint tells you which complexities can pass — often it tells you the intended algorithm.
Measured on this Mac:
100 million simple ops: 62 msThat's faster than 10⁸/s, because the loop is trivial and the JIT is excellent. Real solutions do more per step (memory access, objects, bounds checks), so 10⁸ per second is a safe planning number, and typical time limits are 1–2 seconds.
Under the Hood — the table to memorise#
| Largest n | Allowed complexity | Typical techniques |
|---|---|---|
| n ≤ 10–11 | O(n!) | all permutations, brute force |
| n ≤ 20–25 | O(2ⁿ), O(2ⁿ · n) | all subsets, bitmask DP (Bit Manipulation (coming soon)) |
| n ≤ 100–500 | O(n³) | triple loops, some DP, Floyd–Warshall |
| n ≤ 2,000–5,000 | O(n²) | pairs, 2-D DP |
| n ≤ 10⁵–10⁶ | O(n log n) or O(n) | sorting, heaps, binary search, hash maps, two pointers, sliding window |
| n ≤ 10⁸ | O(n) with tiny constants | a single simple pass |
| n ≥ 10⁹ / 10¹⁸ | O(log n) or O(1) | binary search on the answer, maths formulas, fast exponentiation |
How to use it in an interview:
Read the constraint. "n up to 10⁵" → quadratic is 10¹⁰ steps → too slow → the answer must be about O(n log n) or better. This rules out brute force before you write it.
Say the brute force and its complexity out loud anyway ("the naïve way checks every pair, O(n²), which is 10¹⁰ for 10⁵ — too slow"), then improve it. Interviewers want to see this reasoning.
After coding, state time and space complexity without being asked, and name the dominant step.
If an interviewer gives no constraints, ask: "roughly how large can the input be?"
Check yourself
4 questionsn ≤ 10⁵. Is an O(n²) solution acceptable? Why?
Show answerHide answer
No — about 10¹⁰ operations, roughly 100 seconds at 10⁸/s; aim for O(n log n) or O(n).
n ≤ 18. What complexity class is probably intended?
Show answerHide answer
Exponential, like O(2ⁿ) — subsets or bitmasks.
n ≤ 10¹⁸. What must the algorithm be?
Show answerHide answer
O(log n) or O(1) — you can't even loop to n.
The interviewer gives no size. What do you do?
Show answerHide answer
Ask for the expected input size (and still state the complexity of your solution).