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

What is the difference between time complexity and space complexity?

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

Time complexity measures how the number of operations grows with n; space complexity measures how much EXTRA memory the algorithm needs as a function of n (not counting the input itself).

Explanation

They can trade off: the duplicate-detection trick that uses a HashSet turns an O(n^2)-time, O(1)-space nested scan into an O(n)-time solution that costs O(n) extra space. Interviewers often ask you to state both.

Common mistakes
  • Ignoring extra memory such as copies or recursion depth.
  • Treating time and space complexity as the same thing.

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