foundation

Big O notation

Reason about time, memory, amortized behavior, and best, average, and worst cases.

Big O describes growth, not exact speed. Interviewers expect you to define the input size, identify the dominant operation, and explain why constants disappear only after the model is clear. The main trade-off is precision versus usefulness: Big O hides hardware and constant factors, but it lets you compare algorithms under growth.

Example:

					const seen = new Set();
for (const value of values) seen.add(value);
				

The loop is O(n) average time and O(n) memory because each input value is processed once and stored at most once.

On interviews: separate best, average, worst, and amortized cases. Mention when a nested loop is still O(n) because pointers move monotonically.

Common pitfalls: calling every nested loop O(n²); ignoring memory; forgetting amortized behavior for dynamic arrays and hash tables.

Checklist:

  • Define n before estimating.
  • Name time and memory.
  • Separate best, average, worst, and amortized cases.
  • State the data-structure assumption.