A complete beginner-friendly guide: understand why TLE happens, how to count instructions, and exactly how to optimize your code step by step.
Before you can fix TLE, you need to understand what happens when you hit "Submit."
Every submission runs on a real server. That server executes roughly 1,000,000,000 (10⁹) simple instructions per second. If your code needs more instructions than that within the time limit (usually 1–2 seconds), the judge kills your process and says: Time Limit Exceeded.
An "instruction" is any single action your CPU performs. Let's break down a real loop.
i=0 ×1 (once only)i <= n ×n timesi++ ×n timesarr[i] ×n timessum ×n timesDrag the sliders to instantly see if your code passes or TLEs — and get a tip.
If you have 100 instructions per element and N=10⁶, that's 10⁸ — borderline. Here are the exact techniques to reduce that count. Click each tab.
arr.find(), arr.includes(), or arr.indexOf() inside a loop — that's secretly O(n²). Replace with a Map or Set.Follow these steps on every problem — before writing a single line of code.
1 ≤ N ≤ ???. That number is your guide. Don't skip it!str += char (use array.join), arr.splice(), arr.unshift(), arr.includes(), JSON.stringify() — all secretly O(n)!Each bar shows total operations. Anything beyond the 10⁹ limit = TLE.
🟢 Green = safe | 🟡 Yellow = borderline | 🔴 Red = TLE
👆 Click any row to see the math, a real example, and why it works.
| Input N | Use This ✅ | Avoid ❌ | Quick Tip |
|---|
Can you pick the right approach just from the constraint?