Every good problem statement ends with a constraints section, and most beginners skip it. That's a mistake: the constraints quietly tell you how fast your solution needs to be — and therefore which approaches are even worth trying.
A typical judge (and a typical interviewer's expectation) allows roughly one second, and a computer does somewhere around 100 million simple operations in that time. So plug the largest n into your algorithm's complexity and check whether the result stays under about 10⁸.
n = 100,000 (10⁵)
O(n²) → 10¹⁰ steps ✗ about 100 seconds: too slow
O(n log n) → 1.7 × 10⁶ ✓
O(n) → 10⁵ ✓Most array problems you'll meet have n up to 10⁵ or 10⁶, which means the brute force O(n²) is too slow and you're expected to find something O(n) or O(n log n). When n is small, the problem is telling you that an exponential search is fine — often because the problem is genuinely hard otherwise.
It isn't just n. The values change which technique works:
long in Java and C++.Problem: find two numbers in nums that add up to target.
Constraints: 2 ≤ n ≤ 10⁴, -10⁹ ≤ nums[i] ≤ 10⁹n ≤ 10⁴ → O(n²) is about 10⁸ steps: borderline, likely accepted, but an interviewer will ask for better.int. Use long for the sum.Two lines of constraints, and you already know the intended solution is O(n) with a hash map — which is exactly the best Two Sum solution in Most asked interview questions.
Before writing any code, say out loud: "n is up to ___, so I need about ___ or better. Values can / can't be negative, sorted / not sorted." Ten seconds of reading saves you from building a beautiful solution that is too slow.