Counting steps and Big-O intuition
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Count the dominant operations an algorithm performs as a function of input size.
- Express that count with Big-O notation and drop constants and lower-order terms.
- Rank common growth rates from constant to quadratic.
Overview
#Count the work, not the clock
Wall-clock time depends on the machine. Instead count how many times the dominant operation runs as the input grows. Let n be the input size.
function containsDuplicate(values) {
for (let i = 0; i < values.length; i++) {
for (let j = i + 1; j < values.length; j++) {
if (values[i] === values[j]) return true;
}
}
return false;
}
The inner comparison runs about n * (n - 1) / 2 times. As n grows the n^2 part dominates, so we say the time complexity is O(n^2).
The rules
- Drop constants: 5n + 20 becomes O(n).
- Keep only the fastest-growing term: n^2 + n becomes O(n^2).
- Big-O describes an upper bound as n grows, so it usually describes the worst case.
Growth rates you will meet
| Notation | Name | Example |
|---|---|---|
| O(1) | constant | array index lookup |
| O(log n) | logarithmic | binary search |
| O(n) | linear | one pass over a list |
| O(n log n) | linearithmic | comparison sorting |
| O(n^2) | quadratic | nested loops over the same list |
| O(2^n) | exponential | exploring every subset |
Space complexity
Space complexity counts extra memory beyond the input. A single running variable is O(1). A copy of the list is O(n). A memo table can be O(n) or more.
Reading complexity off the code
One loop over n items is O(n). A loop inside a loop, each running n times, is O(n^2). A loop that halves its range each step is O(log n). A recursive call that splits the input in two and does O(n) work per level is O(n log n).
Practice next
- Reduce the duplicate check to O(n) using a hash set.
- Estimate the space complexity of the hash-set version.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
How many times does the inner comparison run when values has length 4 in the nested-loop duplicate check?
starter.javascriptfor (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) compare(values[i], values[j]);Reveal the reference answer
reference.javascripti = 0 compares 3 pairs, i = 1 compares 2, i = 2 compares 1, i = 3 compares 0. Total 3 + 2 + 1 + 0 = 6 = 4 * 3 / 2.Authored expected output: 6
The count is n * (n - 1) / 2, which is O(n^2).
Exercise 2Predict
What is the time complexity of this loop?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptfor (let i = 1; i < n; i = i * 2) { work(); }Exercise 3Explain
Explain why n^2 + 100n + 5000 is written as O(n^2).
Reveal the reference answer
reference.textFor large n the n^2 term grows faster than the linear and constant terms, so it dominates the total. Big-O ignores constant factors and lower-order terms because they do not change how the cost scales.Authored expected output: The fastest-growing term dominates; constants and lower-order terms are dropped.
This is why two algorithms in the same Big-O class can still differ by a constant factor that matters in practice.
Common misconceptions
#- Big-O measures seconds. It measures growth as input size increases, independent of hardware.
- O(2n) is different from O(n). Constants are dropped, so they are the same class.
- The average case is always the same as the worst case. They can differ, for example in hash lookup.
Follow-up practice
#- dsa-from-zero-arrays-and-indexing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.