AlgoTales · Roadmap

Big-O, Big-Ω, Big-Θ

Beginner · 8 min · code in PHP, JavaScript, Java and Go

Where you will meet this in real life

Big-O is a way of describing how much slower a piece of code gets as the amount of data grows. It ignores seconds on your laptop and asks a better question: if the data becomes ten times bigger, does the work stay the same, grow ten times, or explode?

Practical use cases

  • Web app: Product search that freezes after launch. A small shop tests its search box with 200 products and it feels instant. After importing 50,000 products, every keystroke loops through all of them and the page hangs. Thinking in Big-O tells you in advance that checking every product on every keystroke will not survive growth, so you add an index or a lookup table before launch.
  • Web app: Duplicate check inside a loop. A CSV import page checks each new email against the list of all existing emails with in_array(). With 1,000 rows it is fine; with 100,000 rows the import runs for minutes because the work is rows times emails. Swapping the list for a PHP array keyed by email makes each check one step, and the import finishes in seconds.
  • System: Database queries on a growing table. A query that filters a table with no index must look at every row, so it gets slower in step with the table. With an index the database jumps almost straight to the match. Developers use this mental model to decide which columns deserve an index before the table reaches millions of rows.

The story: The Locker Room Mystery

Mira's school had 100 lockers, and one morning the principal announced: "Someone left a red notebook in a locker. Find it!" Mira did the obvious thing. She opened locker 1, then locker 2, then locker 3... By the time she found it in locker 87, the bell had rung twice.

Her friend Kabir smiled. "My cousin's school keeps a register: name → locker number. He looks at the register once, walks straight to the locker, and is done. Even if his school had a million lockers, it would take the same time."

Then came the plot twist. Next year Mira's school grew to 10,000 lockers. Her method became 100 times slower. Kabir's cousin's method? Exactly the same speed. That, right there, is what Big-O is about: not "how many seconds", but "how much slower do I get when the crowd grows?"

How to spot this problem, and where you need it

Your feature works perfectly with test data but gets slower and slower as real users and records pile up, and you cannot tell whether the cause is the server or your own code.

Signs you need it

  • A loop sits inside another loop, both walking over data that keeps growing, such as orders and order items or users and permissions.
  • Page load time doubles when the data doubles, or worse, grows much faster than the data does.
  • The code calls in_array(), array_search() or a database query once per item inside a foreach loop.
  • A problem statement gives limits like a million items and a time limit, hinting that a slow approach will not pass.

Where to use it in a project

  • Reviewing any report, export or import feature before it meets production-sized data.
  • Choosing between a list, a keyed array or a database index for lookups that run many times per request.

The solution

Count how many times your code touches each item when the data doubles. If every item is touched once, you are fine. If every item is compared with every other item, look for a smarter structure: a keyed array for instant lookup, a sorted list with halving search, or a database index. Rewrite the hot spot, then test again with ten times more data to confirm that the growth is gentle.

Payoff: Before: an import of 100,000 rows takes minutes because of nested checks. After: one pass with a lookup array finishes in a few seconds.

Explain it like I am 8

Imagine hunting for your red crayon. If every crayon lives in a labelled box, you grab it in one go. If they are mixed into a giant toy pile, you dig toy by toy. Big-O does not count seconds. It asks: "if the pile gets 10 times bigger, does my digging get 10 times longer, or stay the same?"

How it works, step by step

  1. Pick the thing that grows. Usually the number of items, called n (lockers, users, products).
  2. Count the big repeated work. Ignore small stuff. One extra step does not matter when the crowd is huge.
  3. Name the shape. Same effort always = O(1). Halving each time = O(log n). Visit everyone once = O(n). Everyone meets everyone = O(n²).
  4. Know the three words. Big-O is the "at most this slow" promise (worst case). Big-Ω is "at least this much work" (best case, the lucky day). Big-Θ is "exactly this kind of work" when best and worst agree.

The code in four languages

PHP

<?php
// O(n): open every locker until we find it
function findLinear(array $lockers, string $item): int {
    foreach ($lockers as $number => $content) {
        if ($content === $item) return $number;   // lucky? stop early (best case O(1))
    }
    return -1;                                    // unlucky? we checked all n (worst case)
}

// O(1): ask the register (a PHP array key lookup)
function findWithRegister(array $register, string $name): ?int {
    return $register[$name] ?? null;              // same effort for 10 or 10 million
}

$register = ['Mira' => 87, 'Kabir' => 12];
echo findWithRegister($register, 'Mira');         // 87

JavaScript

// O(n): open every locker until we find it
function findLinear(lockers, item) {
  for (let i = 0; i < lockers.length; i++) {
    if (lockers[i] === item) return i;   // best case O(1), worst case O(n)
  }
  return -1;
}

// O(1): ask the register (an object / Map lookup)
const register = { Mira: 87, Kabir: 12 };
function findWithRegister(name) {
  return register[name] ?? null;         // same effort for 10 or 10 million
}

console.log(findWithRegister('Mira'));    // 87

Java

import java.util.*;

public class Main {
    // O(n): open every locker until we find it
    static int findLinear(String[] lockers, String item) {
        for (int i = 0; i < lockers.length; i++) {
            if (lockers[i].equals(item)) return i;   // lucky? stop early (best case O(1))
        }
        return -1;                                   // unlucky? we checked all n (worst case)
    }

    // O(1): ask the register (a HashMap lookup)
    static Integer findWithRegister(Map<String, Integer> register, String name) {
        return register.get(name);                   // same effort for 10 or 10 million (null if absent)
    }

    public static void main(String[] args) {
        Map<String, Integer> register = new HashMap<>();
        register.put("Mira", 87);
        register.put("Kabir", 12);
        System.out.println(findWithRegister(register, "Mira"));   // 87
    }
}

Go

package main

import "fmt"

// findLinear: O(n), open every locker until we find it.
func findLinear(lockers []string, item string) int {
	for i, content := range lockers {
		if content == item {
			return i // lucky? stop early (best case O(1))
		}
	}
	return -1 // unlucky? we checked all n (worst case)
}

// findWithRegister: O(1), ask the register (a map lookup).
func findWithRegister(register map[string]int, name string) (int, bool) {
	number, ok := register[name] // same effort for 10 or 10 million
	return number, ok
}

func main() {
	register := map[string]int{"Mira": 87, "Kabir": 12}
	if number, ok := findWithRegister(register, "Mira"); ok {
		fmt.Println(number) // 87
	}
}

Time: O(1) < O(log n) < O(n) < O(n²) · Extra space: Depends on the method. Read left to right: each one is slower than the last when n grows.

Watch out for

  • Thinking Big-O means seconds. A fast computer running an O(n²) method still loses to a slow phone running O(n) once n gets large.
  • Forgetting hidden loops: in_array(), array_search() or .includes() inside a loop quietly turn O(n) into O(n²).

Quick check

  1. A school doubles its lockers. Kabir's register lookup takes…
    • Twice as long
    • The same time
    • Half the time
  2. Mira opens every locker one by one. What is that called?
    • O(1)
    • O(log n)
    • O(n)
  3. Which word describes the lucky "found it in locker 1" day?
    • Best case (Big-Ω)
    • Worst case (Big-O)
    • Never happens