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
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;
}
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 */ }
}
š 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)