Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Algorithms & Competitive Programming — Alpha School

Lesson 2: Big-O, Big-Theta and Big-Omega

How do you compare two algorithms without running them? Timing them on your computer is unreliable: the result depends on your machine, the language, and what else is running. The standard answer is asymptotic analysis — describing how the work grows as the input grows, while ignoring the constant factors that differ between machines. The language for that is Big-O notation, and it is the single most important idea in this course.

When we say an algorithm is O(n), we mean its running time grows at most in proportion to the input size n. Reading a list once is O(n). Checking every pair of items is O(n^2), because there are roughly n times n pairs. Halving the problem at each step, as binary search does, is O(log n). And an algorithm that does the same work no matter how big the input is — such as looking up a value in a dictionary — is O(1), constant time. The letter n always means the size of the input, and the expression after the O describes the shape of the growth, not an exact count of steps.

How running time grows with input size

Running time as the input grows (sketch, not to scale) n time O(1) constant O(log n) O(n) linear O(n²) quadratic

The curves are a sketch of the shapes, not measured timings. The key point is the shape: doubling n doubles an O(n) algorithm's work, but quadruples an O(n²) algorithm's work.

O, Theta and Omega — three ways to describe growth

Big-O is an upper bound: it says the work grows at most this fast. Its siblings describe the other sides. Big-Omega is a lower bound: the work grows at least this fast. Big-Theta is a tight bound: the work grows exactly this fast, up to constant factors — the upper and lower bounds match. In practice, most contest discussion uses Big-O to describe the worst case, and Big-Theta when the best and worst cases grow the same way. You will mostly need Big-O, but knowing the other two stops you from being confused when a problem statement or editorial uses them.

A few working rules make Big-O practical. Drop constant factors: O(2n) is just O(n). Drop lower-order terms: O(n^2 + n) is O(n^2), because the squared term dominates as n grows. And judge an algorithm by its worst case unless the problem says otherwise — an algorithm that is usually fast but sometimes slow is a risk in a contest. The next lesson turns these rules into a skill you can apply to real loops and recursive functions.

Going further

An algorithm can have different complexities in different cases. Linear search is O(n) in the worst case — the item is last, or absent — but O(1) in the best case, when the item is first. Binary search is O(log n) worst case and O(1) best case. When people quote a single complexity for an algorithm, they almost always mean the worst case, because that is the guarantee you can rely on. The average case is also meaningful, but it depends on what the inputs typically look like, which is harder to pin down — and in a contest, the test data is usually chosen to hit the worst case.

One detail that confuses beginners: the base of the logarithm does not matter in Big-O notation. Whether you write log base 2, base 10 or base e, the values differ only by a constant factor — and Big-O ignores constant factors. That is why textbooks simply write O(log n) without naming a base. The same reasoning explains why O(n log n) appears so often: it is the cost of splitting a problem in half, log n times, while doing linear work at each level — the signature of every efficient divide-and-conquer algorithm.

A useful exercise is to estimate before you measure. Pick a simple loop you have written, work out its Big-O by hand, and predict how much slower it will run if the input doubles — then double the input and time it. If the prediction matches, your analysis is sound; if it does not, something is hiding in the code — perhaps a hidden O(n) operation inside the loop, such as a membership test on a list, or a string concatenation that copies the whole string each time. That gap between prediction and measurement is where real understanding lives, and closing it is the fastest way to get good at analysis.

Key Terms

Big-O
An upper bound on how an algorithm's running time grows with input size, ignoring constant factors.
Big-Theta
A tight bound: the running time grows exactly this fast, up to constant factors.
Big-Omega
A lower bound on how an algorithm's running time grows with input size.
Asymptotic analysis
Studying how running time grows as the input gets large, rather than measuring exact seconds.
O(1)
Constant time: the work does not grow with the input size.
O(log n)
Logarithmic time: the work grows very slowly, typically by halving the problem each step.
O(n²)
Quadratic time: the work grows with the square of the input size, as when checking every pair.
Worst case
The slowest behaviour of an algorithm over all inputs of a given size; what Big-O usually describes.

Discussion Questions

  1. Why is Big-O more useful than timing a program with a stopwatch? What does a stopwatch measure that Big-O ignores, and vice versa?
  2. If algorithm A is O(n) and algorithm B is O(n²), is A always faster in practice? When might B win on a small input?
  3. Why do we drop constant factors and lower-order terms? What are we giving up, and what do we gain?

Practice Questions

  1. What does it mean to say an algorithm is O(n)?
  2. A loop runs n times, and inside it another loop runs n times. What is the complexity?
  3. What is the difference between Big-O and Big-Theta?
  4. Looking up a key in a Python dictionary is O(1) on average. What does that mean in plain words?
  5. Simplify O(3n² + 5n + 100) to standard Big-O form.
Show answers
  1. Its running time grows at most in proportion to the input size n. Doubling the input roughly doubles the work, ignoring constant factors.
  2. O(n²). The inner loop runs n times for each of the n outer iterations, giving about n times n steps.
  3. Big-O is an upper bound; Big-Theta is a tight bound where upper and lower growth match. An algorithm can be O(n²) but Theta(n) if its worst case is quadratic but its typical case is linear.
  4. The lookup takes about the same time no matter how many keys the dictionary holds. Constant time does not mean instant — it means the time does not grow with size.
  5. O(n²). Drop the constant factor 3 and the lower-order terms 5n and 100; the n² term dominates as n grows.
Keep reading to complete this lesson