Why Your Code Gets TLE (Time Limit Exceeded)

I'm Shubham (@shubhamsinghbundela), I'm a Software Engineer, a Full-stack developer, a tech enthusiast, and a technical writer here on @Hashnode. I have a strong zeal to share my acquired knowledge and I am also willing to learn from others.
When solving problems on platforms like Codeforces or LeetCode, many beginners face a common issue:
TLE (Time Limit Exceeded)
Let’s understand this in the simplest way possible.
The Hidden Rule: 10⁸ Operations per Second
Most competitive programming platforms assume:
In 1 second, your code can perform around 10⁸ operations
This is not exact, but a safe assumption.
What Does This Mean?
Case 1:
If you run a loop:
for (int i = 0; i < 10^8; i++)
It will roughly take 1 second
Case 2:
If:
n = 10^9
and you run:
for (int i = 0; i < n; i++)
Total operations = 10⁹
Rule: 1 sec we can do only 10⁸ operation
Time ≈ 10 seconds (Too slow → TLE)
So What’s the Problem?
In many problems (like Range Sum Query), constraints are:
q ≤ 10^5
If you solve each query using a loop:
For each query, we are running a loop from L to R.
for (int i = 0; i < q; i++) {
int ans = 0;
for (int i = l; i <= r; i++) {
ans+=arr[i];
}
}
Worst Case Scenario
Number of queries =
q = 10^5In worst case, each query covers the full array
L = 1, R = n = 10^5
So for one query, loop runs:
10^5 times
Total Operation
Now we do this for every query:
Total operations = 10^5 (queries) × 10^5 (loop per query)
= 10^10 operations
Convert Operations → Time
We know:
1 second ≈ 10^8 operations
Now divide total work:
10^10 ÷ 10^8 = 100 seconds
Final Result
Your program needs ~100 seconds
But time limit is 1 second
So it will definitely give TLE
Now the Real Question
At what time complexity should we write our solution?
We already know:
1 second ≈ 10⁸ operations
So now the goal is simple:
Total operations in your code should be ≤ 10⁸
How to Decide Time Complexity from Constraints
We use the value of N (input size) to decide what kind of loop we can run.
Quick Cheat Sheet
| N (Input Size) | Allowed Complexity |
|---|---|
| 10² | O(n²), O(n³) |
| 10³ | O(n²) |
| 10⁵ | O(n log n), O(n) |
| 10⁷ | O(n) borderline |
| 10⁹ | O(log n) |
| 10¹⁸ | O(log n) or O(1) |
Case 1: N = 10⁵
If you write:
for (int i = 0; i < n; i++)
Operations = 10⁵ → Very fast
If you write:
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
Operations = 10⁵ × 10⁵ = 10¹⁰ TLE
Case 2: N = 10⁹
for (int i = 0; i < n; i++)
Operations = 10⁹ → Too slow
So what can we do?
Use logarithmic solution
Example:
while (n > 0) {
n = n / 2;
}
Operations ≈ log₂(10⁹) ≈ 30
Case 3: N = 10¹⁸
You cannot even loop once up to N
So only allowed:
O(1)
O(log n)
Example:
while (n > 0) {
n /= 2;
}
~60 operations only
Key Insight
Bigger the constraint → Smaller the allowed time complexity
Example Thinking (How Top Coders Think)
Instead of memorizing formulas, good programmers do quick mental math before coding.
Let’s break each case properly
Case 1: n = 10⁵
Ask yourself:
If I run one loop →
10⁵operations (very fast)If I run n log n →
10⁵ × log₂(10⁵) ≈ 10⁵ × 17 = 1.7 × 10⁶(safe)If I run nested loop (n²) →
10⁵ × 10⁵ = 10¹⁰(too slow)
Conclusion:
For
n = 10⁵, you can safely use:
O(n)
O(n log n)
O(n²)
Case 2: n = 10⁹
Now think:
One loop →
10⁹operationsBut limit is - In 1 sec we can iteration 10⁸ only
10⁹ ÷ 10⁸ = 10 seconds
So what to do?
You must avoid looping till n
Instead think:
Can I use math formula?
Can I use binary search?
Can I reduce problem using logarithm?-
Golden Rule
Don’t start coding immediately
First check: Will my solution fit in 10⁸ operations?
Final Takeaway
Constraints tell you how to think
Time complexity tells you what to write
Optimization is just reducing operations




