"What's the Time Complexity?" How to Explain Big-O Out Loud in a Coding Interview

"What's the Time Complexity?" How to Explain Big-O Out Loud in a Coding Interview

You've written the solution. It looks right. Then the interviewer leans back and asks the question that ends almost every coding round: "What's the time and space complexity?"

Many candidates answer with a quick "O(n)" and hope. Sometimes that's right. Often it's half right, missing a sort, a nested loop hidden inside a helper, or the cost of building a string. And even when the number is correct, a bare answer with no reasoning gives the interviewer nothing to grade.

What they want to hear is how you get there: you can look at your own code, find the work that dominates, and say why. This guide gives you a short routine for doing that out loud, with examples you can reuse.

Why interviewers always ask about complexity

The complexity question isn't a formality. It checks a few things at once:

Step 1: Name your variables before you name the answer

"O(n)" only means something once everyone agrees what n is. Start by defining the inputs:

"Let n be the number of words in the list and k be the length of the longest word."

This one sentence prevents the most common mistake: collapsing two independent sizes into one. Graph problems need V and E. Grid problems need rows and columns (m and n). String-matching problems often need the text length and the pattern length. If you name them up front, your final answer is precise, and the interviewer sees that you're thinking about the real shape of the input.

Step 2: Walk the code top to bottom and cost each block

Don't try to see the whole answer at once. Go through the solution block by block and say the cost of each:

"Building the frequency map is one pass, so O(n). Sorting the unique keys is O(u log u), where u is the number of distinct values, at most n. The final loop over the sorted keys is O(u)."

Things that are easy to miss:

Step 3: Add or multiply, then keep the biggest term

Once each block has a cost, combine them:

Saying the unsimplified version first is a good habit. It shows the interviewer your working and gives them a chance to spot anything you've missed before you commit to a final answer.

Step 4: Handle loops and recursion that aren't obvious

Two patterns trip people up most often.

Two pointers or sliding windows with a nested while. It looks like O(n²) because there's a loop inside a loop. Explain why it isn't:

"The inner while moves the left pointer forward, and the left pointer never moves back. Across the whole run it moves at most n times, so the total work is O(n), not O(n²)."

This is amortized reasoning, and naming it out loud is a strong signal.

Recursion. Use a simple formula: number of calls × work per call. For memoized recursion, the number of calls is the number of distinct states:

"There are n × m distinct (i, j) states and each one does constant work after memoization, so O(n · m) time and O(n · m) space for the memo, plus the recursion depth on the call stack."

For plain backtracking, be honest that it's exponential and give the branching factor: "Each position has two choices, so roughly O(2ⁿ) subsets, and copying each one is O(n), so O(n · 2ⁿ) overall."

Step 5: Don't forget space, and say what counts

Space complexity questions usually get rushed. Cover three things:

  1. Auxiliary data structures like hash maps, sets, queues, and memo tables.
  2. The recursion stack, which is O(depth) even if you allocate nothing else. A recursive DFS on a skewed tree is O(n) space.
  3. The output, and whether you count it. Say: "Excluding the output list, extra space is O(1)." Interviewers differ on this, so stating your convention avoids confusion.

A full example answer

Here's what a complete, calm answer sounds like for a "group anagrams" solution that sorts each word and uses the sorted word as a hash key:

"Let n be the number of words and k the maximum word length. For each word I sort its characters, which is O(k log k), and insert into a hash map with average O(1) work plus O(k) to hash the key. So the loop is O(n · k log k) time. Space is O(n · k) for the map, since every character ends up stored once in a key or a group. If we wanted to drop the log factor, we could use a 26-length character count as the key instead, which makes it O(n · k)."

Notice the structure: variables, per-step cost, combined total, space, and a short note on a possible improvement. That last line often answers the next question before it's asked.

What to do if you're not sure

Sometimes you'll look at your code and the complexity isn't clear. That's fine. Don't guess a number and hope. Think out loud instead:

"I think it's O(n log n) because of the sort, but let me double-check the inner loop... it only runs once per distinct key, so the sort still dominates."

If you realize mid-answer that your solution is slower than you thought, say it plainly: "Actually, the in check on that list is O(n), so this is O(n²). Switching it to a set brings it back to O(n)." Catching your own mistake is a positive signal. Defending a wrong number is not.

Common mistakes to avoid

How to practice this

The complexity routine only becomes automatic with repetition. After every practice problem, before you check the editorial, say your full answer out loud: variables, each block's cost, the total, and space. Record yourself once or twice. You'll quickly hear where you rush or skip steps.

It also helps to practice in conditions closer to a real interview: talking while you think, with a timer, and someone or something asking follow-up questions. If you want a structured way to rehearse explaining your reasoning, including complexity, during mock sessions, Luna Interview is a Chrome side-panel interview copilot built for that kind of prep.

FAQ

Do I need to give the complexity even if the interviewer doesn't ask? Yes, briefly. Stating it when you finish shows you understand your solution and often saves the interviewer a question.

Is it OK to say "average case" for hash maps? Yes. "Average O(1) lookups, worst case O(n) with heavy collisions" is a precise and expected answer.

Should I state best case, average case, and worst case? Lead with the worst case unless the algorithm is usually described by its average case, like quicksort or hash maps. Mention the others only when they differ in a way that matters.

What if my complexity answer is wrong? If the interviewer pushes back, re-walk the code block by block out loud. Most mistakes show up as soon as you cost each step separately.