SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm PerformanceBig O & Time ComplexitySpace ComplexityLogarithms and ComplexityProblem Solving ApproachAlgorithm Efficiency and Problem Solving
Big O Notation

Big O & Time Complexity

Learn how Big O describes time complexity. Compare O(1), O(log n), O(n), and other growth rates with examples in JavaScript and Python.

Big O notation gives an upper bound on how fast an algorithm’s cost can grow as the input gets larger. For example, scanning an array of n items takes O(n) time because the work grows at most linearly. For the formal definition, see MIT’s notes on asymptotic complexity.

Counting work as input grows

Let n be the number of items in a list. Reading its first item takes one indexed read. Finding its minimum by scanning a nonempty list takes n − 1 comparisons. If the list doubles in size, the first operation still takes one read and the scan needs about twice as many comparisons.

A linear scan also fits a looser quadratic upper bound, but O(n) describes its growth more precisely. Name the case you are analyzing. A search may finish on its first comparison in the best case and inspect every item in the worst case.

Compare the values of seven representative functions at input sizes 2, 4, and 8. For eight items, n² = 64, while 2ⁿ = 256. Equal distances on the plot multiply the value by the same factor. Actual operation counts depend on the algorithm and can include constant factors.

At n = 8 the representative functions 1, log base two of n, n, n log base two of n, n squared, 2 to the n, and n factorial are 1, 3, 8, 24, 64, 256, and 40,320.

For the algorithm examples below, assume fixed-size items and constant-cost operations on each item, such as one indexed read.

GrowthMeaningExample pattern
Constant O(1)Runtime does not grow with input size.One operation
Logarithmic O(log n)Runtime grows slowly as the input is halved repeatedly.Binary search
Linear O(n)Runtime grows with the number of input items.One loop
Linearithmic O(n log n)Runtime grows in proportion to n × log n.Merge sort
Quadratic O(n²)Runtime grows with the square of the input size.Nested loops
Exponential O(2ⁿ)Two choices per item double the number of possible outcomes.Include or skip each item
Factorial O(n!)Runtime grows factorially with each additional item.Visiting every possible ordering

Constant growth: O(1)

An operation with constant growth has a fixed upper bound on its work regardless of input size. For a nonempty array, reading its first item uses one indexed access.

function getFirstItem(items) {
  return items[0]; // Always performs a single operation
}

Whether the input has a few items or many items, this pattern still performs one operation.

Logarithmic growth: O(log n)

Logarithmic algorithms cut the problem size down (usually in half) with every step.

function binarySearch(sortedItems, target) {
  let left = 0;
  let right = sortedItems.length - 1;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (sortedItems[mid] === target) return mid;

    if (sortedItems[mid] < target) {
      left = mid + 1; // Halves the search space
    } else {
      right = mid - 1; // Halves the search space
    }
  }
  return -1;
}

Each unsuccessful comparison discards the midpoint and about half the candidates. Doubling the list adds at most one midpoint inspection to the worst case.

Linear growth: O(n)

A single loop that processes each input item has linear growth.

function printAllItems(items) {
  for (const item of items) {
    console.log(item); // Runs once for each item: n times
  }
}

If the input contains n items, the loop performs work n times.

Linearithmic growth: O(n log n)

Linearithmic algorithms split the problem into smaller subproblems logarithmically (log n levels) while performing linear work (n) across each level.

For example, merging sorted runs of length 1 into runs of length 2 touches each item once. Merging those runs into runs of length 4 touches them all again. Eight items take three such merge levels: 1 → 2 → 4 → 8. The logarithms lesson explains how to count the levels.

Quadratic growth: O(n²)

Nested loops can create quadratic growth when each loop goes through the input.

function printAllPairs(items) {
  for (const first of items) {
    for (const second of items) {
      console.log(first, second); // Runs n * n times
    }
  }
}

If the input contains n items, the inner operation is performed n × n times. As the input grows, quadratic runtime grows much faster than linear runtime.

Exponential growth: O(2ⁿ)

Imagine choosing toppings from a list. Each topping can be included or skipped, independently. One topping gives two choices, two toppings give four subsets, and three give eight. With n toppings there are 2ⁿ distinct subsets. Visiting each subset and doing one fixed-cost check per visit takes exponential time. Writing out every topping in every subset also costs time.

Factorial growth: O(n!)

Suppose three people, A, B, and C, stand in a line. There are three choices for the first place, two for the second, and one for the last: 3 × 2 × 1 = 6 possible lines. For n distinct people there are n! permutations. Testing every permutation takes at least that many visits. If you also copy out each full length-n ordering, writing the results alone takes O(n × n!) time.

Comparing growth rates

These representative functions grow increasingly quickly for large inputs:

1 → log n → n → n log n → n² → 2ⁿ → n!

Simplifying Big O Expressions

Big O focuses on how running time grows as input size increases. To simplify an expression:

  1. Remove constant factors.
  2. Remove lower-order terms.
  3. Keep the term with the greatest growth.

For example:

O(3n² + 5n + 7) → O(n²)

The constant 3 is removed, and the lower-order terms 5n and 7 are ignored. The resulting bound is O(n²).

Estimating Complexity

Decide what each loop counts before combining its costs. A scan of n items followed by a scan of m other items visits n + m items. Comparing every item of the first list with every item of the second visits n × m pairs. Keep both variables unless the problem tells you how the two lengths are related.


Key Takeaways

  • Big O Notation: A mathematical model that describes how execution time or memory consumption grows as input size (n) increases.
  • Time Complexity: Measures the growth in the number of basic operations an algorithm performs as input size scales, independent of hardware or environment.
  • Drop Constant Factors: Multipliers and constants are removed because Big O focuses on asymptotic growth trends rather than exact hardware execution time (O(3n) → O(n)).
  • Drop Lower-Order Terms: Only the fastest-growing (dominant) term is kept when evaluating combined operations (O(n² + 5n) → O(n²)).

How is this lesson?

Previous lessonAlgorithm PerformanceNext lessonSpace Complexity
Edit on GitHubReport on GitHub
On this page
%

On this page

Counting work as input growsConstant growth: O(1)Logarithmic growth: O(log n)Linear growth: O(n)Linearithmic growth: O(n log n)Quadratic growth: O(n²)Exponential growth: O(2ⁿ)Factorial growth: O(n!)Simplifying Big O ExpressionsEstimating ComplexityKey Takeaways