Big O notation describes how the running time or memory usage of an algorithm grows as the input size (n) grows. It focuses on the growth rate in the worst case (unless stated otherwise), ignoring constants and lower-order terms, so you can compare algorithms independently of hardware. Common complexities (from fastest to slowest growth) - O(1) — constant: accessing an array element by index, a hash table lookup on average, pushing onto a stack. - O(log n) — logarithmic: binary search, operations on balanced binary search trees. Doubling the input adds only one more step. - O(n) — linear: scanning a list, finding the maximum, summing elements. - O(n log n) — linearithmic: efficient sorting algorithms (merge sort, heap sort; quicksort on average, and the sorts used by .NET and Java libraries). - O(n²) — quadratic: nested loops over the same data, bubble sort, comparing every pair. - O(2^n) — exponential: naive recursive Fibonacci, generating all subsets. - O(n!) — factorial: generating all permutations, brute-force travelling salesman. For n = 1,000,000: O(log n) is about 20 steps, O(n) is a million, O(n log n) about 20 million, and O(n²) a trillion — which is why choosing the right algorithm matters far more than micro-optimisation. How to calculate time complexity 1. Count how the number of basic operations grows with n. int Sum(int[] a) { int total = 0; // O(1) foreach (var x in a) total += x; // runs n times → O(n) return total; // O(1) } Total: O(1) + O(n) + O(1) = O(n). 2. Drop constants and lower-order terms. - O(2n + 10) becomes O(n); O(n² + n) becomes O(n²); O(n/2) becomes O(n). 3. Sequential steps add; nested steps multiply. for (i = 0; i < n; i++) { ... } // O(n) for (j = 0; j < m; j++) { ... } // O(m) → O(n + m) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { ... } // O(n × n) = O(n²) 4. Loops that halve or double the remaining work are logarithmic. while (n > 1) { n /= 2; } // O(log n) Binary search halves the search space each step → O(log n). 5. Recursion: count calls × work per call. - Naive Fibonacci: fib(n) = fib(n-1) + fib(n-2) makes about 2^n calls → O(2^n). With memoisation each value is computed once → O(n). - Merge sort: splits into two halves (log n levels) and does O(n) merging per level → O(n log n). 6. Consider hidden costs inside library calls. - list.Contains(x) inside a loop is O(n) per call, making the loop O(n²); using a HashSet makes lookups O(1) on average and the loop O(n). - String concatenation in a loop can be O(n²) because strings are copied; StringBuilder makes it O(n). Space complexity - Measures extra memory used: an algorithm creating a copy of the input uses O(n) space; recursion depth also counts (recursive DFS uses O(depth) stack space). Best, average and worst case - Quicksort: O(n log n) average, O(n²) worst case with poor pivots. - Hash table lookup: O(1) average, O(n) worst case with many collisions. - Related notations: Big Omega (lower bound) and Big Theta (tight bound); in interviews "Big O" usually means the worst-case upper bound. Interview summary: "Big O describes how an algorithm's cost grows with input size. Count operations, drop constants and lower-order terms, add sequential steps, multiply nested ones, treat halving loops as log n, and watch hidden costs in library calls and recursion."
Ravindra Wadile · · 0 likes