{"post":{"slug":"big-o-notation-explained-2026","title":"Big-O notation explained in 2026: complexity without the maths degree","excerpt":"Big-O tells you how an algorithm scales as input grows — not how fast it runs today. Learn the seven classes you actually encounter and how to read them in real code.","category":"code","date":"March 26, 2026","author":"ByteLedger Team","content":"<span class=\"dropcap\">B</span>ig-O is the language programmers use to discuss whether an algorithm will still work when the data grows by 10×, 100×, or 1 000×. It is not about timing a function on your laptop — it is about the *shape* of the growth curve. Once you can read it, you can have an informed opinion about almost any algorithm in any codebase.\n\n## What changed in 2026\n\n- **LLM-assisted coding makes complexity awareness more important, not less.** AI assistants generate O(n²) solutions to problems that have O(n log n) solutions. Recognising the difference is now a core developer skill.\n- **Data volumes keep growing.** With pgvector embedding tables at 50 M+ rows and event streams at billions of records, choosing O(n log n) over O(n²) is a real-world cost and latency difference.\n- **Interview culture shifted.** FAANG-style whiteboard complexity drills softened, but product companies still filter on \"can you spot a nested loop problem?\" Practical complexity reasoning matters.\n- **GPU parallelism does not remove algorithmic complexity.** A GPU runs O(n²) faster but the scaling curve is the same. Algorithmic improvements compound on top of hardware.\n\n## The seven complexity classes\n\n| Class | Name | Example algorithm |\n|---|---|---|\n| O(1) | Constant | Hash table lookup, array index |\n| O(log n) | Logarithmic | Binary search, balanced BST |\n| O(n) | Linear | Linear scan, single loop |\n| O(n log n) | Linearithmic | Merge sort, heap sort, good sort |\n| O(n²) | Quadratic | Bubble sort, nested loop comparison |\n| O(2^n) | Exponential | Naive recursive subset generation |\n| O(n!) | Factorial | Brute-force travelling salesman |\n\nEverything above O(n log n) is \"be careful at scale.\" Everything above O(n²) is \"don't ship to production with large n.\"\n\n## Reading Big-O in real code\n\n**O(1) — constant:**\n```python\nlookup = {\"alice\": 42, \"bob\": 7}\nval = lookup[\"alice\"]   # O(1) regardless of dict size\n```\n\n**O(n) — single loop:**\n```python\ndef find_max(arr):\n    m = arr[0]\n    for x in arr:   # iterates n times\n        if x > m:\n            m = x\n    return m\n```\n\n**O(n²) — nested loop:**\n```python\ndef has_duplicate(arr):\n    for i in range(len(arr)):\n        for j in range(i + 1, len(arr)):   # inner loop = O(n) × outer O(n)\n            if arr[i] == arr[j]:\n                return True\n    return False\n# Fix: use a set → O(n) time, O(n) space\n```\n\n**O(log n) — halving the problem:**\n```python\ndef binary_search(arr, target):\n    lo, hi = 0, len(arr) - 1\n    while lo <= hi:\n        mid = (lo + hi) // 2\n        if arr[mid] == target:\n            return mid\n        elif arr[mid] < target:\n            lo = mid + 1     # discard left half\n        else:\n            hi = mid - 1    # discard right half\n    return -1\n```\n\n## Dropping constants and lower-order terms\n\nBig-O deliberately ignores constants because hardware and language differences swamp them at any fixed n. When n grows without bound, only the dominant term matters.\n\n- `O(3n + 100)` → **O(n)**\n- `O(n² + n)` → **O(n²)**\n- `O(n log n + n²)` → **O(n²)** (n² dominates)\n- `O(500)` → **O(1)**\n\n## Space complexity\n\nEvery algorithm also has a space complexity — how much extra memory it uses relative to input size.\n\n| Algorithm | Time | Space |\n|---|---|---|\n| In-place sort (heapsort) | O(n log n) | O(1) |\n| Merge sort | O(n log n) | O(n) |\n| Recursive DFS (tree) | O(n) | O(h) — h = height |\n| Memoised Fibonacci | O(n) | O(n) |\n| Hash set dedup | O(n) | O(n) |\n\nTrading space for time is one of the most common optimisation patterns. See [Hash tables explained in 2026](/blog/hash-tables-explained-2026) for the canonical example.\n\n## How to spot the complexity class\n\n1. **Count loops.** One loop over n → O(n). Two nested loops over n → O(n²). Be careful: a loop inside a recursive call multiplies.\n2. **Check if you discard half the problem each step.** Halving → O(log n) component.\n3. **Look for sorts.** `Array.sort()`, `sorted()`, `.sort()` are all O(n log n).\n4. **Check data structure operations.** Hash table get/set: O(1). Sorted set insert: O(log n). Array insert at front: O(n).\n5. **Count recursive branches.** Two branches with no memoisation → likely O(2^n) or worse.\n\n## How to pick the right complexity\n\n1. **n < 1 000?** Almost anything works; pick the readable solution.\n2. **n ~ 10 000–100 000?** O(n²) starts to hurt; target O(n log n) or better.\n3. **n > 1 M?** O(n log n) is fine; O(n²) is unusable; chase O(n) or O(log n) if possible.\n4. **Is the bottleneck I/O, not CPU?** Big-O may not be your problem — profile first.\n\n## Common mistakes\n\n**Counting the wrong n.** If n is the number of nodes and e is the number of edges, graph DFS is O(n + e), not O(n). Identify what \"n\" refers to precisely.\n\n**Ignoring built-in complexity.** Python's `in` on a list is O(n). `in` on a set is O(1). Swapping them inside a loop can change O(n²) to O(n).\n\n**Optimising O(n) to O(n/2) and calling it a win.** That is still O(n). Find a genuinely different algorithm.\n\n**Conflating average and worst case.** Quick sort is O(n log n) average but O(n²) worst case on sorted input. Use `sorted()` (Timsort) for production — it is O(n log n) worst case.\n\n## What to skip\n\n- **Premature micro-optimisations inside O(n) code** — profiling beats guessing. See [How to profile slow code in 2026](/blog/how-to-profile-slow-code-2026).\n- **Hand-rolling O(n log n) sorts** — every standard library has Timsort or equivalent; use it.\n- **Memorising Big-O tables without understanding structure** — derive it from the code by counting; you will remember it.\n\n## FAQ\n\n**What is the difference between O, Ω, and Θ?**\nBig-O is an upper bound (worst case). Omega (Ω) is a lower bound (best case). Theta (Θ) is a tight bound (both). In interviews and daily work, \"Big-O\" informally means the tight worst-case bound.\n\n**Does O(1) mean fast?**\nNot necessarily. O(1) means constant-time — the time does not grow with n. That constant could be 1 ms or 500 ms. A slow O(1) operation can dominate a fast O(n) loop for small n.\n\n**How do I handle multiple inputs?**\nUse separate variables. An algorithm that loops over two arrays of sizes n and m is O(n + m), not O(n²).\n\n**Is Big-O enough for system design?**\nNo. Big-O ignores cache locality, I/O, concurrency, and network latency. Use it to eliminate obviously bad choices, then measure with real data.\n\n## Where to go next\n\n- [Hash tables explained in 2026](/blog/hash-tables-explained-2026)\n- [Binary search explained in 2026](/blog/binary-search-explained-2026)\n- [Dynamic programming explained in 2026](/blog/dynamic-programming-explained-2026)","hue":311,"metaTitle":"Big-O Notation Explained 2026: Complexity Made Simple","metaDescription":"Clear 2026 guide to Big-O notation: the seven complexity classes, how to spot them in code, space complexity, and when optimisation actually matters.","keywords":["big o notation explained 2026","time complexity 2026","algorithm complexity guide","o n log n explained","space complexity vs time complexity","big o for beginners","how to calculate big o","algorithm analysis 2026","coding interview complexity","asymptotic notation explained"],"tldr":["**Big-O measures growth rate, not raw speed** — O(n log n) sort beats O(n²) sort at large n even if the constant factor is worse.","**The seven classes you need** — O(1), O(log n), O(n), O(n log n), O(n²), O(2^n), O(n!) cover almost every algorithm you will interview on or ship in production.","**Drop constants and lower-order terms** — O(3n + 100) is O(n); the simplification is intentional.","**Space complexity matters as much as time complexity** — a recursive DFS is O(h) space where h is tree height.","**Skip:** optimising O(n) to O(n/2) — that is still O(n); look for an algorithmic improvement instead."],"readTime":"5 min read","wordCount":1108},"related":[{"slug":"what-is-big-o-notation-2026","title":"What is Big O notation in 2026? A plain-language guide with examples","excerpt":"Big O notation describes how an algorithm slows down as its input grows. This guide explains the common complexity classes, shows real examples, and clears up what Big O does and does not measure.","category":"code","date":"May 13, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":95},{"slug":"how-to-learn-data-structures-2026","title":"How to learn data structures in 2026: a practical roadmap that actually works","excerpt":"Data structures are the vocabulary of every coding interview and production system. Here is the 2026 roadmap to learn them efficiently and durably.","category":"code","date":"March 31, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":307},{"slug":"hash-tables-explained-2026","title":"Hash tables explained in 2026: internals, collisions, and real use cases","excerpt":"Hash tables power the O(1) lookups behind Python dicts, JavaScript objects, and Redis. Here is how they work, where they fail, and when to use them.","category":"code","date":"April 14, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":312}],"sameCategory":[{"slug":"bazel-build-explained-2026","title":"Bazel Explained: What Hermetic Builds Actually Cost","excerpt":"Bazel delivers reproducible, precisely cached builds and demands that you describe everything. Here is what you get, what it costs, and who should say no.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":145},{"slug":"graphql-federation-2026","title":"GraphQL Federation: One Graph, Many Teams","excerpt":"Federation composes independently-owned subgraphs into a single API. Here is what it solves, the operational cost it adds, and when a simpler gateway is enough.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":300},{"slug":"hermetic-builds-explained-2026","title":"Hermetic Builds Explained: Same Inputs, Same Output","excerpt":"A hermetic build depends only on declared inputs. Here is what that property gives you, the common leaks that break it, and how to move toward it incrementally.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":130},{"slug":"monorepo-tooling-compared-2026","title":"Monorepo Tooling Compared: What You Actually Need","excerpt":"Monorepo tools solve task orchestration and caching, not repository layout. Here is what each tier provides and how to tell when you have outgrown the simple option.","category":"code","date":"August 19, 2026","author":"ByteLedger Team","readTime":"3 min read","hue":150}],"newer":{"slug":"best-wrist-rests-2026","title":"Best wrist rests in 2026: what material and height actually matter","excerpt":"The wrong wrist rest creates more strain than none at all. In 2026 the differences between foam, gel, and memory foam are well-understood — here is what to buy.","category":"tech","date":"March 26, 2026","author":"ByteLedger Team","readTime":"4 min read","hue":236},"older":{"slug":"binary-search-explained-2026","title":"Binary search explained in 2026: the algorithm and its real-world uses","excerpt":"Binary search reduces a sorted-array lookup from O(n) to O(log n) — but it is also the core idea behind database indexes, Git bisect, and feature flags.","category":"code","date":"March 26, 2026","author":"ByteLedger Team","readTime":"5 min read","hue":314}}