Video in progress

What does Big O notation measure, and why is it the first thing asked in an interview?

Big O shows how fast an algorithm's time or memory grows as the input size n grows. It gives you the shape of the growth, not exact milliseconds, which makes it the quickest way to compare solutions.

📖 Answer

Big O doesn't answer "how many seconds does this code take". It tells you something else: if the data grows 10 times, how many times does the work grow?

Say you're looking for duplicates among 1000 users. Two nested loops are O(n²), roughly a million comparisons. With a HashSet it's O(n), around a thousand. At n = 1,000,000 the difference becomes the difference between seconds and hours.

The rules are simple:

  • constants are dropped: 3n + 5 → O(n);
  • the fastest-growing term stays: n² + n → O(n²);
  • unless said otherwise, people usually mean the worst case.

In an interview, after you write a solution, the interviewer almost always asks "what's the complexity?". State both time and extra memory, for example "time O(n), memory O(n), because I used a HashMap".