Big-O

All about Big O Notation

Ever wondered why your code slows down as you add more data? Or why some algorithms are faster than others, even if they do the same thing? The answer is often hidden in something called Big O notation.

In this article, we’ll break down Big O in the simplest way possible. You’ll learn what it means, why it matters, and how to spot it in your own code. No math degree required!

What is Big O Notation?

Big O notation is a way to describe how the time (or memory) your code needs grows as the input gets bigger. Instead of measuring how long a function takes to run, Big O helps you predict how your code will behave with small, medium, or huge amounts of data.

Think of it as a speedometer for your code’s efficiency.

Big-O

Why Should You Care?

  • Interviewers love it. Big O is a favorite topic in coding interviews, especially for companies like Google, Amazon, and Meta.
  • It helps you write better code. Knowing Big O lets you choose the right algorithm for the job.
  • It saves you headaches. You’ll avoid slowdowns and bugs that only show up with big data.

The Four Most Common Big O Categories

Let’s look at the four Big O types you’ll see most often:

4

1. Constant Time – O(1)

No matter how big your input is, the code always takes the same amount of time.

function getFirstItem(arr) {
  return arr[0];
}

Whether arr has 10 items or 10 million, this function is always fast. That’s O(1).

2. Linear Time – O(n)

The time grows in direct proportion to the input size.

function printAll(arr) {
  for (let item of arr) {
    console.log(item);
  }
}

If you double the number of items, the function takes twice as long. That’s O(n).

3. Quadratic Time – O(n^2)

The time grows with the square of the input size. This usually happens with nested loops.

function printPairs(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      console.log(arr[i], arr[j]);
    }
  }
}

If you have 10 items, it prints 100 pairs. With 100 items, it prints 10,000 pairs! That’s O(n^2).

4. Logarithmic Time – O(log n)

The time grows slowly, even as the input gets huge. This happens when you can cut the problem in half each step, like in binary search.

function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}

With 1,000,000 items, binary search only needs about 20 steps to find something. That’s O(log n).

Visualizing Big O

Visualize

Imagine you’re running a race. O(1) is like sprinting a short distance—always quick. O(n) is jogging a longer track. O(n^2) is running laps inside laps—exhausting! O(log n) is like teleporting closer to the finish line with each step.

Real-Life Coding Examples

Finding an Item in a List

function contains(arr, target) {
  for (let item of arr) {
    if (item === target) return true;
  }
  return false;
}

This is O(n) because you might have to check every item.

But if you use a Set:

const items = new Set(["apple", "banana", "cherry"]);
items.has("banana"); // O(1)

Lookups in a Set are O(1)!

Sorting: Bubble Sort

Bubble sort is a classic example of O(n^2):

function bubbleSort(arr) {
  let swapped;
  do {
    swapped = false;
    for (let i = 0; i < arr.length - 1; i++) {
      if (arr[i] > arr[i + 1]) {
        [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
        swapped = true;
      }
    }
  } while (swapped);
  return arr;
}

Caching Results

Sometimes you can speed things up by remembering previous results (caching):

const cache = new Map();
function factorial(n) {
  if (cache.has(n)) return cache.get(n);
  if (n === 0) return 1;
  const result = n * factorial(n - 1);
  cache.set(n, result);
  return result;
}

Conclusion

Big O notation helps you write code that stays fast—even as your data grows. Whether you’re prepping for interviews or just want to be a better coder, understanding Big O is a superpower.

If you found this guide helpful, share it with a friend or bookmark it for your next interview prep session!

next-time

Similar Posts