DEV Community

Ankit Maheshwari
Ankit Maheshwari

Posted on • Originally published at bitveen.com

Time Complexity & Big-O Notation Explained Simply

Handling 10 inputs is easy. Handling 10 lakh inputs is where real skill shows. That's what time complexity measures.

⚔ What is Time Complexity?

It measures how the number of operations grows as input size grows — not seconds, not milliseconds.

šŸ“Š The 4 Big-O You Must Know

O(1) — Constant Time

function getFirst(arr) { return arr[0]; } // Always 1 step
Enter fullscreen mode Exit fullscreen mode

O(n) — Linear Time

function findMax(arr) {
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) max = arr[i];
  }
  return max;
}
Enter fullscreen mode Exit fullscreen mode

O(log n) — Logarithmic Time

Each step halves the problem. 1,000,000 elements → only ~20 steps. Example: Binary search.

O(n²) — Quadratic Time

for (let i = 0; i < n; i++) {
  for (let j = 0; j < n; j++) { /* nƗn ops */ }
}
Enter fullscreen mode Exit fullscreen mode

šŸ“‰ Growth Comparison Table

n O(1) O(log n) O(n) O(n²)
10 1 3 10 100
1,000 1 10 1,000 1,000,000
10,000 1 13 10,000 100,000,000

At 10,000 elements — O(n²) runs 100 million ops vs O(n)'s 10,000.

šŸŽÆ Interview Tip

Always analyse time complexity before and after optimising. Interviewers ask: "What's the Big-O?"


Part 4 of the Bitveen DSA Series. Originally published at bitveen.com

Top comments (0)