Big O notation
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Big O notation is a shorthand way to describe how the step count of an algorithm grows when the input size n becomes very large. Instead of tracking every single addition or assignment, Big O names the overall growth curve.
Growth rates: O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ)
Big O notation describes how the running time or memory of an algorithm grows as the input size n grows, ignoring constant factors, for example O(n) or O(n log n).
Almost every algorithm you will meet in coding rounds falls into one of six growth rates, listed here from fastest (least work) to slowest (most work):
O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)
Example 1 · Comparing all six growth rates in Java for increasing n
public class GrowthChartDemo { public static void printRow(int n) { long o1 = 1; long oLogN = Math.round(Math.log(n) / Math.log(2)); long oN = n; long oNLogN = n * oLogN; long oN2 = (long) n * n; long o2N = (n <= 20) ? (1L << n) : -1L; // 1L << n is 2^n
System.out.println("n=" + n + " | O(1)=" + o1 + " | O(log n)=" + oLogN + " | O(n)=" + oN + " | O(n log n)=" + oNLogN + " | O(n^2)=" + oN2 + " | O(2^n)=" + (o2N == -1 ? "huge" : o2N)); }
public static void main(String[] args) { printRow(8); printRow(16); printRow(1024); }}n=8 | O(1)=1 | O(log n)=3 | O(n)=8 | O(n log n)=24 | O(n^2)=64 | O(2^n)=256 n=16 | O(1)=1 | O(log n)=4 | O(n)=16 | O(n log n)=64 | O(n^2)=256 | O(2^n)=65536 n=1024 | O(1)=1 | O(log n)=10 | O(n)=1024 | O(n log n)=10240 | O(n^2)=1048576 | O(2^n)=huge
What this code does
- When
ndoubles from8to16,O(log n)increases by just1(3 -> 4),O(n)doubles (8 -> 16),O(n^2)quadruples (64 -> 256), andO(2^n)squares (256 -> 65,536). - At
n = 1024,O(n log n)is only10,240steps, whereasO(n^2)is already over one million steps.
Example 2 · Examples of O(1), O(n), and O(n^2) methods in one class
public class BigOExamplesDemo { // O(1) — Constant time: checks first and last element regardless of array length public static boolean endsMatch(int[] nums) { if (nums.length == 0) { return true; } return nums[0] == nums[nums.length - 1]; }
// O(n) — Linear time: two sequential passes (2n steps -> drop constant 2 -> O(n)) public static int countEqualToMax(int[] nums) { if (nums.length == 0) { return 0; } int max = nums[0]; for (int x : nums) { if (x > max) { max = x; } } int count = 0; for (int x : nums) { if (x == max) { count++; } } return count; }
// O(n^2) — Quadratic time: nested loop checks n * n pairs public static int countAllEqualPairs(int[] nums) { int pairs = 0; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if (nums[i] == nums[j]) { pairs++; } } } return pairs; }
public static void main(String[] args) { int[] sample = {5, 2, 5, 1, 5}; System.out.println("O(1) endsMatch : " + endsMatch(sample)); System.out.println("O(n) countEqualToMax : " + countEqualToMax(sample)); System.out.println("O(n^2) allEqualPairs : " + countAllEqualPairs(sample)); }}O(1) endsMatch : true O(n) countEqualToMax : 3 O(n^2) allEqualPairs : 11
What this code does
endsMatchalways performs two array lookups and one comparison, so it isO(1)even ifnumshas a million elements.countEqualToMaxruns two separate loops of lengthn(2nsteps total), which simplifies toO(n)after dropping the constant2.
O(1)Constant:arr[0],map.get(key)(average), or the Gauss sum formula.O(log n)Logarithmic: Binary search in a sorted array.O(n)Linear: A single loop scanning an array once.O(n log n)Linearithmic,O(n^2)Quadratic,O(2^n)Exponential: Sorting, nested pair loops, and trying all subsets.
Constant time O(1) means the step count stays flat no matter how large n grows. Logarithmic time O(log n) cuts the problem in half on each step (like binary search). Linear time O(n) visits each element a fixed number of times. Linearithmic time O(n log n) is the speed of fast sorting (Arrays.sort). Quadratic time O(n²) comes from checking all pairs in a nested loop, and exponential time O(2ⁿ) doubles its work every time n increases by 1.
Dropping constants and smaller terms
A method that does 3n² + 10n + 50 steps is still just O(n²). The code below shows why the small terms and the constant 3 stop mattering as n grows.
First, drop lower-order terms: keep only the fastest-growing term (3n^2 + 10n + 50 becomes 3n^2). Second, drop constant multipliers: ignore fixed numbers multiplying that term (3n^2 becomes O(n²)).
Example 3 · Why constants and lower-order terms fade away as n grows
public class DropConstantsDemo { // Exact step count: T(n) = 2*n*n + 10*n + 50 public static void compareTerms(long n) { long quadraticPart = 2 * n * n; long linearAndConstPart = 10 * n + 50; long total = quadraticPart + linearAndConstPart; long percentQuadratic = (quadraticPart * 100) / total; System.out.println("n=" + n + " -> 2n^2 is " + percentQuadratic + "% of total work"); }
public static void main(String[] args) { compareTerms(10); compareTerms(100); compareTerms(10_000); }}n=10 -> 2n^2 is 57% of total work n=100 -> 2n^2 is 95% of total work n=10000 -> 2n^2 is 99% of total work
What this code does
- At
n = 10, the lower-order terms10n + 50still make up43%of the steps. - By
n = 10,000, the2n^2term accounts for over99.9%of the work, which is why simplifying2n^2 + 10n + 50toO(n²)captures the true scaling behavior.
Suppose a method does 3n^2 + 10n + 50 elementary steps. When n = 100,000, the 3n^2 term is 30,000,000,000, while 10n + 50 is only 1,000,050—less than 0.004% of the total work. Because the biggest power of n completely dominates for large n, Big O notation uses two simplification rules.
- Start with the exact step formula, for example
5n + 120. - Drop the smaller term
+ 120->5n. - Drop the constant multiplier
5->O(n). - Another example:
n^2 / 2 - n / 2-> keepn^2 / 2-> drop1 / 2->O(n^2).
O(n * m), neither n nor m is a fixed constant!Reading a growth chart
To feel the difference between these six growth rates, compare how many operations each formula performs as n grows from 10 to 1,000:
At n = 10: O(1) = 1, O(log₂ n) ≈ 3, O(n) = 10, O(n log₂ n) ≈ 33, O(n²) = 100, and O(2ⁿ) = 1,024. At n = 1,000: O(1) = 1, O(log₂ n) ≈ 10, O(n) = 1,000, O(n log₂ n) ≈ 10,000, O(n²) = 1,000,000, and O(2ⁿ) has more than 300 digits—more than the number of atoms in the observable universe.
- When
ndoubles (2x),O(n)doubles (2x),O(n^2)quadruples (4x), andO(log n)adds just1extra step! - An
O(2^n)algorithm is only practical for tiny inputs liken <= 20(2^20is about1,048,576). - An
O(n^2)algorithm usually becomes too slow aroundn = 10,000to100,000.
Big O as an upper bound (simplified model)
In everyday software engineering and interviews, we use Big O as a simplified model for how an algorithm's step count grows in the worst case, written as the tightest upper bound.
In formal computer science textbooks, Big O (O) means 'grows no faster than' (an upper bound), Big Omega (Ω) means 'grows at least as fast as' (a lower bound), and Big Theta (Θ) means 'grows at the exact same rate' (both upper and lower bound). In coding interviews, when someone asks for the Big O of a single loop from 0 to n - 1, always give the tight bound O(n), not a loose bound like O(n²).
- Simplified interview model: Give the tightest growth rate for the worst case.
- Constant factors still exist in real life: an
O(n)loop doing 50 heavy operations per step can be slower onn = 50than a tinyO(n^2)loop, even thoughO(n)wins for largen.
Next step
Check your understanding
10 quick questions