📌 Time Complexity: A Mental Roadmap You Won’t Forget

“I’ve learned time complexity before... so why do I keep forgetting it?”

If that sounds like you — welcome. This blog is not a deep theoretical dive. It’s a practical cheat sheet for your brain — built for developers who get the idea of Big O but want a repeatable framework to retain it and apply it confidently.

🧠 What Even Is Time Complexity?

Time complexity is not about actual time.
It answers one question:

“How does the number of operations grow as input size increases?”

It's about scalability, not speed in milliseconds.

🔁 The Roadmap: How to Think About Time Complexity Every Time

Whenever you’re analyzing code, follow this 3-step process:


Step 1: Identify the Input Size (n)

What is growing in your program?

  • Array length?

  • Number of digits?

  • Tree depth?

  • String length?

You can’t reason about time complexity unless you know what n is.

Step 2: Count the Number of Work Units

Ask:

  • How many times does the key operation run?

  • Are there loops? Nested loops?

  • Is the problem being split into smaller parts (recursion or divide & conquer)?

🔎 Ignore constants — don’t sweat +1, /2, etc. Just focus on the dominant term.

Step 3: Map It to a Time Complexity Class

Time ComplexityWhat It MeansThink of it as...
O(1)ConstantInstant
O(log n)LogarithmicCutting in half
O(n)LinearLoop over everything
O(n log n)LinearithmicEfficient sorting
O(n²)QuadraticNested loops
O(2ⁿ), O(n!)Exponential / FactorialDon’t run this in prod 😅

🎯 Apply It Like a Pro — with Examples

Let’s look at examples and apply our roadmap:


🔹 O(1) — Constant Time

function getFirst(arr) {
  return arr[0];
}
  • Step 1: Input size = arr.length

  • Step 2: Only one operation — return

  • Step 3: ✅ O(1)

🔹 O(n) — Linear Time

function sum(arr) {
  let total = 0;
  for (let i = 0; i < arr.length; i++) {
    total += arr[i];
  }
  return total;
}
  • Input = n elements

  • One loop → n steps
    → ✅ O(n)

🔹 O(n²) — Quadratic Time

function printAllPairs(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      console.log(arr[i], arr[j]);
    }
  }
}
  • Two nested loops → n × n operations
    → ✅ O(n²)

🔹 O(log n) — Logarithmic Time

function binarySearch(arr, target) {
  let left = 0, right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
}
  • Each step cuts search space in half
    → ✅ O(log n)

🔹 O(n log n) — Linearithmic Time

Sorting algorithms like Merge Sort, Quick Sort (avg case)
These divide the input (log n) and process all elements (n)
→ ✅ O(n log n)

🔹 O(2ⁿ) — Exponential Time

function fib(n) {
  if (n <= 1) return n;
  return fib(n - 1) + fib(n - 2);
}
  • Every call spawns 2 more → grows fast
    → ❌ Not scalable → O(2ⁿ)

🛠️ Common Patterns You Can Memorize

PatternTime Complexity
Single loop over n itemsO(n)
Loop inside loopO(n²)
Recursive halving (binary)O(log n)
Divide + process all partsO(n log n)
Brute-force combinationsO(2ⁿ), O(n!)

💬 Wrapping Up

Time complexity isn’t something you memorize once — it’s something you internalize with patterns. Think of it as a mental lens for understanding code performance.

And every time you forget, come back to this 3-step roadmap:

  1. What’s the input?

  2. How many operations scale with input?

  3. What Big O class does that match?

0
Subscribe to my newsletter

Read articles from Dhanraj Pai Raiturkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhanraj Pai Raiturkar
Dhanraj Pai Raiturkar

I'm a Software Engineer 2 at Blue Yonder, working primarily with JavaScript, TypeScript, React, Node.js, and MongoDB. With over 3 years of experience, I've built scalable frontend and backend solutions, including working with AWS serverless technologies like Lambda and DynamoDB in my previous role. I hold a Bachelor's in Computer Applications (BCA) and a Master's in Computer Applications (MCA), and I'm passionate about building clean, performant applications and continuously learning new things in the world of web development.