Browse curriculum

Big-O Notation

Big-O notation is the language used to describe the performance of algorithms: how their cost grows as the input grows.

QUESTION 1 / 4 · SCORE 0

YOUR CALL

for (let i = 0; i < n; i++) {
    console.log(i);
}

What is the time complexity of this snippet?

Show

Intuition

Imagine you're buying a car. You don't ask "exactly how many milliseconds does it take to go 0-60?". You ask "is it a sports car, a sedan, or a truck?". Big-O is that classification system for algorithms.

Concept

  • Upper Bound: Big-O describes the ceiling of growth. It won't get worse than this.
  • Asymptotic Analysis: We only care about large inputs (N → ∞).
  • Drop Constants: O(2n) is just O(n). O(n² + n) is just O(n²).

How it Works

The Rules:

  • Loops: Multiply complexity by N.
  • Nested Loops: Multiply inner by outer (N * N = N²).
  • Halving: If loop variable is divided/multiplied, it's Logarithmic.

Step-by-Step Breakdown

Play the Quiz Game to practice identifying complexities!

When to Use

  • When designing any system to ensure scalability.
  • In technical interviews!

When NOT to Use

  • When N is guaranteed to be very small (e.g., sorting 5 items).

How to Identify

"Analyze the complexity", "Will this scale?", "Optimize".

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

What is the difference between O, Θ and Ω?

Big-O is an upper bound, "no worse than". Big-Ω is a lower bound, "no better than". Big-Θ is both at once, a tight bound. Interview usage almost always means the tight bound even when it says O.


Is O(n) always faster than O(n²)?

Only once N is large enough. Big-O ignores constant factors, so a quadratic routine with a tiny constant can beat a linear one on small inputs. That is why fast sorts switch to insertion sort on small subarrays.


Does Big-O mean the worst case?

Not by itself. Big-O bounds a function, and which case you are bounding is a separate choice. You have to say whether the bound is on the best, average or worst case.