"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:
- Whether you understand what you wrote. If you can't say how many times the inner loop runs, the interviewer can't tell if you designed the solution or just pattern-matched it.
- Whether you can compare options. Complexity is the shared language for trade-offs. Without it, "this one is faster" is just a feeling.
- Precision under mild pressure. The question comes at the end, when you're tired. A calm, structured answer is a strong closing signal.
- A setup for the next follow-up. "Can you do better?", "Can you do it in O(1) space?", and "What if the input doesn't fit in memory?" all build on your complexity answer.
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:
- Built-in calls that aren't free.
sort,slice,substring,list.index,inon a list, and string concatenation in a loop all cost more than one step. - Helper functions. If a helper loops over the input, every call to it inside a loop multiplies.
- Copying. Passing a slice or a copied array into each recursive call adds both time and space.
- Hash map assumptions. It's fine to say "average O(1) lookups" for a hash map. Saying "average" shows you know the worst case is different.
Step 3: Add or multiply, then keep the biggest term
Once each block has a cost, combine them:
- Sequential blocks add: O(n) + O(n log n) becomes O(n log n).
- Nested work multiplies: a loop over n items that does O(k) work each time is O(n · k).
- Drop constants and smaller terms, but only at the end, and say so: "That's O(2n + log n), which simplifies to O(n)."
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
whilemoves 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:
- Auxiliary data structures like hash maps, sets, queues, and memo tables.
- The recursion stack, which is O(depth) even if you allocate nothing else. A recursive DFS on a skewed tree is O(n) space.
- 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
- Answering with one letter when there are two inputs. "O(n)" for a grid problem is usually wrong. Say O(m · n).
- Forgetting the sort. If you sorted anywhere, check whether O(n log n) dominates.
- Calling every nested loop O(n²). Check whether the inner pointer ever resets.
- Ignoring the call stack. Recursion uses space even without a memo.
- Saying "constant" for string building in a loop. In many languages repeated concatenation copies the string each time.
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.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.