What is Big O notation and what are the common complexities?
Big O notation is a mathematical notation used in computer science to describe the upper bound of an algorithm's growth rate as input size n approaches infinity. It answers the question: how does the time (or space) this algorithm needs scale as the input grows? By convention, Big O focuses on the dominant term and drops constants and lower-order terms -- O(2n + 5) is written O(n) because for large n the linear term dominates.
Big O describes worst-case complexity by default (unless stated otherwise). Omega notation describes best case; Theta notation describes tight bounds (both upper and lower). In interviews, Big O and worst case are almost always what is meant.
The common complexity classes, from best to worst: O(1) -- constant time; the operation takes the same time regardless of input size. Hash table lookups, array index access. O(log n) -- logarithmic; input is halved each step. Binary search, balanced BST operations. O(n) -- linear; every element is touched once. Single-pass array scan, linked list traversal. O(n log n) -- linearithmic; appears in efficient comparison sorts. Merge sort, heap sort, Timsort. O(n^2) -- quadratic; nested loops over the input. Bubble sort, insertion sort on unsorted data. O(2^n) -- exponential; every additional element doubles the work. Recursive Fibonacci without memoization, subset enumeration. O(n!) -- factorial; permutation generation, brute-force traveling salesman.
Space complexity follows the same notation and describes auxiliary memory usage -- the extra memory beyond the input. A recursive algorithm that makes n levels of recursive calls uses O(n) stack space even if it uses no other memory.
Common analysis traps: Big O is asymptotic -- O(n^2) can be faster than O(n log n) for very small n due to constants. Amortized analysis is relevant for operations like dynamic array append, which is O(1) amortized even though occasional resizes are O(n). Average case often differs from worst case -- hash table lookup is O(1) average but O(n) worst case with many collisions.
In interviews, always state both time and space complexity for any algorithm you write.
Candidate can recite and rank the common complexities and gives correct examples for each, but cannot explain amortized complexity, the difference between Big O and Theta, or analyze a non-trivial algorithm on the fly.
Candidate explains asymptotic analysis correctly, drops constants and lower-order terms correctly, distinguishes Big O from Omega and Theta, explains amortized analysis, names all common complexity classes with examples, and can analyze a given algorithm's time and space complexity.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment