Big O Complexities — From Best to Worst

NotationNamen=10n=1,000n=1,000,000
O(1)Constant111
O(log n)Logarithmic31020
O(n)Linear101,0001,000,000
O(n log n)Linearithmic3310,00020,000,000
O(n²)Quadratic1001,000,00010¹²
O(2ⁿ)Exponential1,02410³⁰¹Uncomputable

O(1) — Constant Time

The operation takes the same time regardless of input size.

O(1) examples arr[5] // array index access hashMap.get("key") // HashMap lookup (average) stack.push(x) // stack push queue.poll() // queue dequeue return arr[arr.length-1] // access last element

O(log n) — Logarithmic

The problem is halved with each step. Extremely efficient at scale — 1 million items needs only ~20 steps.

Binary search — O(log n) int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } // Each iteration eliminates half the remaining elements

O(n) — Linear

One operation per element. Double the input, double the time.

Linear search — O(n) for (int i = 0; i < arr.length; i++) { // n iterations if (arr[i] == target) return i; }

O(n log n) — Linearithmic

Common in efficient sorting algorithms. n items × log n work per item.

O(n log n) algorithms Arrays.sort(arr) // Java's Timsort — O(n log n) Collections.sort(list) // Also Timsort Merge Sort // Divide: O(log n) levels × O(n) merge = O(n log n) Heap Sort // O(n log n)

O(n²) — Quadratic

Usually caused by nested loops over the same data. Becomes painfully slow at n > 10,000.

Bubble sort — O(n²) for (int i = 0; i < n; i++) { // outer loop: n for (int j = 0; j < n-i-1; j++) { // inner loop: n if (arr[j] > arr[j+1]) swap(arr, j, j+1); } } // Total: ~n² comparisons // Also O(n²): checking all pairs in an array for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) process(arr[i], arr[j]);

Common Data Structure Time Complexities

Data StructureAccessSearchInsertDelete
ArrayO(1)O(n)O(n)O(n)
Linked ListO(n)O(n)O(1)*O(1)*
HashMapN/AO(1) avgO(1) avgO(1) avg
Binary Search TreeO(log n)O(log n)O(log n)O(log n)
Heap (min/max)O(1) topO(n)O(log n)O(log n)
Stack / QueueO(n)O(n)O(1)O(1)

*Linked list insert/delete at a known node position

Rules for Calculating Big O

  1. Drop constants: O(2n) = O(n), O(500) = O(1)
  2. Drop smaller terms: O(n² + n) = O(n²)
  3. Different variables stay separate: Iterating m items then n items = O(m + n), not O(n)
  4. Nested loops multiply: O(n) inside O(n) = O(n²)
  5. Sequential loops add: O(n) then O(n) = O(n), not O(n²)

💡 Interview Tip

Always state both time and space complexity. For recursive solutions, space complexity includes the call stack depth — a recursive solution with n recursive calls has at minimum O(n) space complexity. Iterative solutions typically have O(1) space.

How We Research and Update This Guide

We test the underlying formula or workflow, compare outputs with reliable references, and revise examples whenever the page content changes.

  • The workflow or formula is tested directly in the tool and compared against independent reference examples.
  • Examples are kept practical so readers can verify the result without hidden assumptions.
  • Pages are revised whenever the interface, calculation flow, or surrounding guidance materially changes.

Frequently Asked Questions — Big O Notation