Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Efficiency & Big-O — Measuring Speed From Zero

Which is faster for large n: O(n) or O(n^2), and why?

Level I · BeginnerEfficiency & Big-O — Measuring Speed From Zero
Short answer

O(n) is far faster at scale. If you make the input 1000x bigger, an O(n) algorithm does about 1000x the work, while an O(n^2) algorithm does about 1,000,000x the work.

Explanation

So a nested-loop O(n^2) solution that looks fine on 100 items can be unusable on a million, and switching to an O(n) approach (often by using the right data structure, like a HashSet) beats any amount of micro-optimizing the slow one.

Common mistakes
  • Saying O(n²) is always slower, even for tiny n.
  • Not explaining the growth difference with a concrete number.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Efficiency & Big-O — Measuring Speed From Zero.

Practise free

More on Efficiency & Big-O — Measuring Speed From Zero