Learn computing fundamentals, programming, algorithms, systems, and software engineering.
Big O notation describes how the time or memory an algorithm uses grows as the size of its input grows. It deliberately ignores constants and small terms, because at large scale the dominant term is what matters: an algorithm that takes 3n + 50 steps is still described as O(n), because doubling the input roughly doubles the work. The most important growth rates to recognize are: O(1) constant time, where the work does not depend on input size; O(log n), where each step cuts the problem in half, like binary search; O(n) linear, where the work grows in proportion to the input; O(n log n), typical of efficient sorting; and O(n^2) quadratic, where nested loops over the input multiply the work. Space complexity asks the same question about memory. A function that creates a copy of the input uses O(n) extra space, while one that only keeps a few counters uses O(1). The practical skill is not memorizing definitions but reading code and predicting its growth. A single loop is usually O(n); two nested loops are usually O(n^2); a loop that repeatedly halves a range is O(log n). Recognizing these patterns lets you choose the right approach before your code slows to a crawl on large inputs.
The same task can be solved by algorithms that behave very differently as the input grows. A function that runs in a second for 1,000 items might take hours for a million if its growth is poor. Complexity analysis predicts this before you ever run the code.
| Notation | Name | What it means |
|---|---|---|
| O(1) | Constant | Same work regardless of input size |
| O(log n) | Logarithmic | Halves the problem each step |
| O(n) | Linear | Grows in proportion to input size |
| O(n log n) | Linearithmic | Efficient sorting |
| O(n²) | Quadratic | Nested loops over the input |
Look at the loop structure, not the names of the functions: one loop is linear, nested loops are quadratic, and halving the range is logarithmic.
Two loops over the same input produce every pair, which is n * n = O(n^2) work.
9
Binary search discards half the remaining elements each step, so it needs only about log2(n) steps.
3
Finding duplicates with nested loops is O(n^2); using a set is O(n). Both are correct, but only one scales.
True True