LearnThatStack Ace your next interview
Big-O & Complexity Analysis · question
Question 5 of 57

How do you work out the time complexity of a simple loop?

beginner
← All Big-O & Complexity Analysis questions
Re-explain

Count how many times the loop body runs as the input grows. A loop that visits every one of n items runs n times. If each pass does a fixed amount of work, the loop is O(n).

for (let i = 0; i < n; i++) {
  total += arr[i]; // runs n times, O(1) each
}
// whole loop: O(n)

The loop bound is what matters, not the constant work inside. A loop from zero to n stays O(n) even if the body does ten small steps.

Look at what the counter depends on. If it runs a fixed number of times regardless of input, that part is O(1). A bound that grows differently, like stopping at the square root of n, changes the class.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Big-O & Complexity Analysis One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. I work it out by counting how many times the loop body runs as the input size n grows.
  2. A loop from zero to n runs n times, and if each pass does a fixed amount of work, the whole loop is O(n).
  3. The bound decides it, not the work inside, so a body with ten small steps still leaves the loop at O(n).
  4. Then I check what the counter depends on, because a loop that runs a fixed number of times whatever the input is O(1), and one that stops at the square root of n is O(sqrt n).
  5. This only holds when each pass really is constant work, so if the body calls something that scans the input, I multiply the pass count by that cost.

Weak answers to avoid

  • Counts the lines inside the body Ten statements in the body don't change the class. Say that the number of passes sets the class and that fixed work per pass drops out as a constant.
  • Assumes every loop is O(n) A loop that doubles its counter or stops at the square root of n is not O(n). Show that you read the bound and how the counter changes each pass.
  • Ignores calls inside the body A body that calls includes or slice on the array is not O(1) per pass. Name that hidden cost and multiply it by the number of passes.
  • States O(n) with no reasoning A bare 'it's O(n)' sounds memorized. Say how many times the body runs and what each pass costs, then give the class.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Big-O & Complexity Analysis cheatsheet.

← Back to all Big-O & Complexity Analysis questions
Pro · $10/mo

50 of 57 Big-O & Complexity Analysis answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime