Guide · coding-prep
How to Explain Big O Time Complexity in a Coding Interview
State the loop structure, count the work per step, then multiply and keep the dominant term. Say the Big-O out loud before you code, then re-verify after. Interviewers grade whether you can reason about cost, not whether you can recite Big-O tables from memory.
By Sam K., Founder, InterviewChamp.AI · Last updated
TL;DR: Walk through your loops, figure out the work each iteration does, multiply nested loops, add sequential ones, and say the dominant term with the reason behind it. Do it twice: when you propose the approach and again after coding, and always state space complexity too. A justified O(n²) usually scores better than a bare O(n), because interviewers grade the reasoning, not the letter.
How do you explain Big-O time complexity in a coding interview?#
Walk through the loops and the work per iteration, multiply them, and state the dominant term. Say it out loud twice: once when you propose your approach, and once after coding to check it. The grade is for whether you can reason about cost. The letter itself comes second. Saying "O(n log n) because of the sort plus a linear sweep" is the move. "I think it's fast" is not.
Big-O notation is shorthand for how the work an algorithm does grows as its input grows, keeping only the term that dominates at scale. You don't need to recite the formal definition in a coding round. You need to use it: point at your loops, count the work, and name the dominant term in one sentence the interviewer can follow.
In the 2026 hiring cycle this matters more than it used to. On a HackerRank or CoderPad screen, the complexity story is often the deciding signal on a problem the candidate otherwise solved. Interviewers grade cost reasoning explicitly now, even on a first phone screen, and in a 35-minute round the 30 seconds you spend stating Big-O cleanly buy back more credit than another edge case would. A clean walkthrough lets you leave having said the answer in your own voice, instead of hoping the silent code spoke for itself. Honestly, this is the single cheapest signal most new grads leave on the table.
When to state complexity during the round#
Two moments matter, and you want to hit both.
- When you propose the approach. Before you write code, say the cost of the plan: "Sorting first, then one pass, so O(n log n) time." It shows you picked the approach on purpose, and it gives the interviewer a chance to steer you before you spend ten minutes on the wrong idea.
- After the code is written. Trace the finished code and restate time and space. This is your verification pass. If the code drifted from the plan (an extra loop, a hidden scan), this is where you catch it.
Saying it at both points also means that if time runs out halfway, your cost reasoning is already on the record.
The common complexities, in plain words#
You'll hear the same handful of shapes again and again. Knowing what each one looks like in code makes the walkthrough much faster.
| Complexity | What it means as input grows | What it usually looks like in code | |---|---|---| | O(1) | Work stays flat | A single hash map lookup, reading an array index | | O(log n) | Work grows slowly | Halving the search space each step, like binary search | | O(n) | Work grows in step with the input | One pass over the array | | O(n log n) | A bit more than linear | Sorting, or a sort followed by a linear sweep | | O(n²) | Work grows with the square of the input | Two nested loops over the same data | | O(2^n) and O(n!) | Work explodes | Trying every subset or every ordering |
The table is a shortcut for spotting the shape. It is not the answer. The interviewer still wants to hear why your code lands in a given row, which is what the rest of this guide covers.
The structure of a complexity claim#
Every Big-O statement in an interview should follow this shape:
"[Dominant term] because of [structural reason]. [Smaller terms] from [the rest of the work]. So the total is [dominant term]."
Examples:
- "O(n log n) because of the sort. The two-pointer sweep after is O(n). So total is O(n log n)."
- "O(n), single pass, constant work per element thanks to the hash map."
- "O(n × m), where n is the number of words, m is the longest word length, and I'm doing constant work per character."
That third example shows how to handle two inputs. When a problem has two sizes that don't depend on each other, keep both variables and say what each one means. Don't squash them into a single n.
The shape forces you to justify the answer rather than assert it. Interviewers ding asserted complexities because they can't tell whether you reasoned or guessed. A justified O(n²) often scores higher than an unjustified O(n).
Asserted vs justified complexity: what the interviewer hears#
The single biggest lever in explaining time and space complexity is justification. The same letter lands differently depending on whether you backed it. Here is how a typical claim plays out both ways:
| Moment in the round | Asserted ("it's O(n)") | Justified ("O(n) because...") | |---|---|---| | You state the time complexity | Interviewer can't tell if you reasoned or memorized | Interviewer hears the structure and credits the reasoning | | Your letter is slightly wrong | Reads as a guess; the whole answer looks shaky | Reads as a small slip in sound reasoning; easy to repair | | Interviewer pushes back on it | You have nothing to fall back on | You re-trace the loops on the record and recover | | You hit a hidden O(n) operation | Often missed entirely | You catch it out loud and adjust the claim | | Partial credit when time runs out | Minimal, looks like luck | Heavy, the cost reasoning is documented | | What gets written on the rubric | "Stated complexity" | "Analyzed complexity correctly and defended it" |
A justified O(n squared) routinely outscores a bare O(n), because the rubric rewards the analysis and not the letter. The same thing decides the rest of the round too. The guide on how to think aloud during a coding interview covers the broader narration habit this plugs into.
How to calculate time and space complexity: the five-step method#
Here is the reliable mechanical method, written out so you can rehearse it. The dominant term is the single fastest-growing piece of the cost, the one that swamps everything else as the input gets large. It's the only term that survives into your final answer.
- Identify each loop. Note whether it iterates n times, log n times, sqrt n times, or some other function of the input. A
whileloop counts too, and so does a recursive call. - For each loop, ask what work is done per iteration. Is the inner work constant, or does it depend on input size?
- Multiply nested loops, add sequential loops. Two nested O(n) loops = O(n²). Two sequential O(n) loops = O(n), since 2n = O(n).
- Keep only the dominant term. O(n² + n log n + n) = O(n²). Don't list the smaller terms in the final statement, but feel free to mention them in your walkthrough as proof of work.
- Repeat the whole pass for space. Run steps 1-4 again for memory.
Hidden iteration: the trap in step 2
The trap most candidates fall into is hidden iteration, an operation that reads like a single step but secretly loops over the data. s in substring_set looks like constant work but is O(k), where k is the substring length. list.insert(0, x) looks like one operation but is O(n) because everything shifts over. Hidden iteration is a common way to state the wrong complexity for code that works, so check each line that looks like a single step. For more on technical interviews, see the Pragmatic Engineer's writing on technical interviews.
The fix is the same fluency you'd use when a coding problem you have never seen makes you reason about cost from scratch: read each line for what it does, not what it looks like.
Worst, average and amortized cases#
When you say a complexity with no qualifier, the interviewer will usually take it as the worst case. If your answer depends on a typical case being much cheaper than the worst one, say both out loud so nobody has to guess which one you meant.
Amortized complexity is what one operation costs on average over a long run of calls, even when a single call now and then is expensive. Some operations look variable-cost but average out to constant over many calls:
- Dynamic array
append: O(1) amortized, O(n) on the resize step. - Hash map insert/lookup: O(1) on average (expected), O(n) worst case if every key collides.
- Stack with lazy compaction: O(1) amortized when the compaction work is paid for by the inserts.
State the amortized analysis when your solution depends on it. "I'm using a hash map, so lookups are O(1) on average; the worst case is O(n) but that's vanishingly unlikely for random keys" is the kind of nuance that separates a passing answer from a strong one. It shows up constantly in Big Tech rounds, where the interviewer wants to hear the worst case stated, not buried.
Space complexity is half the grade#
Plenty of candidates state time and forget space, which costs them a chunk of the rubric. Track two sources of space:
- Auxiliary data structures. The hash map, the visited set, the stack you're maintaining. These are usually obvious.
- Recursion and the call stack. Less obvious. Recursive DFS on a balanced binary tree is O(log n) space; on a degenerate (linked-list-shaped) tree it's O(n). Always say which case you're claiming.
Space is easy to skip, so make a habit of stating it every time, right after time. The Indeed Career Guide's writing on technical interview rubrics is useful further reading on how interviews get graded. Space even comes up in system design rounds, where memory footprint is part of the cost conversation.
When complexity is dominated by something subtle#
Here are three classics where the dominant cost isn't the obvious loop.
- String concatenation in a loop. In Java,
result += s[i]in a loop is O(n²), because each+=copies the whole string. Use aStringBuilderinstead. Other languages may handle this differently, so if you are unsure, build a list and join at the end. - Sorting a list of strings. This is O(n log n × k), where k is the average string length, because each comparison is O(k).
- Set membership on a list.
x in some_listis O(n), not O(1). Converting to a set first is almost always worth it.
When one of these dominates, name it: "The concatenation in the loop is O(n²), let me switch to a list-and-join, which gets us back to O(n)." Catching these out loud is one of the strongest signals you can give in a coding round. It says I think about cost, not just correctness. The same instinct powers the verification pass in the guide on how to test your code in an interview, where you re-walk the loops to confirm the complexity you claimed holds.
How to answer "can you do better?"#
This follow-up comes right after you state your complexity, and it's a chance to show the same reasoning again.
- Name the bottleneck first. Point at the term that dominates: "The nested loop is what makes this O(n²)."
- Ask what would remove it. Often it's a hash map to replace an inner scan, a sort that unlocks two pointers, or a set instead of a list for membership checks.
- Restate the new cost with its reason. Use the same claim shape: new dominant term, because of the new structure.
- Mention the trade. If the faster version uses more memory, say so. Trading space for time is a fine answer as long as you name it.
If you can't see an improvement, say what you tried and why it doesn't help. That still documents the reasoning.
Practicing the complexity walkthrough#
Stating complexity cleanly is a motor skill, not a fact you memorize, and like any motor skill it only sticks with reps and feedback. Pick 5 LeetCode mediums, solve each one, and make yourself say the time and space complexity out loud using the dominant-term sentence shape above. Record it. Most candidates discover they assert far more than they justify, and that they often skip space complexity, which is the exact gap that separates a passing analysis from a strong one.
There's no shortcut here, and no tool can hand you a defensible complexity claim mid-round. Interviewers in 2026 are tuned to catch a recited Big-O the candidate can't back. The honest version is rehearsal until the walkthrough is automatic. If you want structured reps where you narrate complexity against realistic prompts and leave able to say the answer in your own voice, try a timed mock interview run and drill the five-step method. The free plan (60 minutes a month, no card) covers your first few sessions, Pro is $29 a month, and the plans on the pricing page lay out how unlimited timed practice works once you feel the difference. The complexity walkthrough is one of the cheapest signals to fix and one of the most rewarding to get right.
Key terms#
Big-O notation: Shorthand for how an algorithm's work grows as its input grows, keeping only the dominant term. In an interview you use it to reason about cost, not to recite a formal definition.
Dominant term: The single fastest-growing piece of the cost, the one that swamps the rest at scale. It is the only term that survives into your final answer; O(n² + n) collapses to O(n²).
Hidden iteration: An operation that reads like one step but secretly loops over the data: substring checks, front-of-list inserts, set membership on a plain list. A common reason candidates state a wrong complexity for code that works.
Amortized complexity: What one operation costs on average over a long run of calls, even if an occasional call is expensive. A dynamic array append, for example, is O(1) amortized but O(n) on the resize step.
Auxiliary space: The extra memory your algorithm allocates beyond the input: hash maps, visited sets, the stack you maintain. One of the two space costs you must name, alongside the call stack.
Recursion call stack: The hidden space a recursive function uses for its pending frames. Balanced-tree depth-first search costs O(log n) stack space; a degenerate, linked-list-shaped tree costs O(n). An easy source of space complexity to miss.
About the author: Sam K. is the founder of InterviewChamp.AI and writes about the modern tech interview from the inside: what changed, what works for new grads, and where the old playbook fails.
Originally published on InterviewChamp.AI
Frequently asked questions
- When should I state the time complexity, before or after coding?
- Both. Say the expected complexity before coding ('this should be O(n log n) because of the sort plus a linear sweep'), then re-verify it after coding by walking through the loop structure line by line. Stating it twice signals intentional design.
- What if I'm not sure whether it's O(n) or O(n log n)?
- Say so out loud and walk through it. 'There's a sort here, so the dominant term is O(n log n). The rest of the work is linear, so the total is O(n log n).' Reasoning visibly beats guessing, and interviewers grade the analysis, not the guess.
- Do I need to mention amortized complexity?
- Mention it when it changes the headline answer. Hash map insertions, dynamic array appends, and stack-with-amortized-ops are the common cases. Saying 'O(1) amortized, O(n) worst-case' is more accurate than just 'O(1)' and shows depth.
- How do I explain space complexity?
- Same structure: say what data structures grow with the input. 'O(n) space for the hash map; the recursion stack adds O(log n) for the binary tree case but O(n) worst case for a degenerate tree.' Don't forget the call stack. It's the most-missed source of hidden space.
- Is it ever okay to skip stating complexity?
- Almost never in a coding round. Even for trivial brute force, say 'this is O(n²) and I'll improve it.' Skipping the analysis is the most common reason candidates get dinged on questions they actually solved correctly.
- How do you explain Big-O notation in an interview?
- Define it in one sentence, then use it. 'Big-O describes how the work grows as the input grows, keeping only the dominant term.' Then apply it to your code: 'this is O(n), one pass, constant work per element.' Interviewers want to see you reason with Big-O, not recite the definition, so spend ten seconds on the concept and the rest on your actual algorithm.
- What is the most common time-complexity mistake in coding interviews?
- Hidden iteration. Operations that look O(1) but secretly loop: string concatenation in a loop (O(n²) in many languages), set membership on a plain list (O(n)), or inserting at the front of an array (O(n)) are the top reason candidates state a wrong complexity for code that actually works. Name the hidden cost out loud the moment you write the line.
- Do I need to explain space complexity too, or just time?
- Both, in 2026. Space complexity is one of the most-skipped items on interview rubrics, so stating it is free signal. Name the data structures that grow with the input and the recursion call stack, for example, 'O(n) space for the hash map, plus O(log n) for the recursion stack on a balanced tree.' Candidates who state space score higher than candidates with identical code who only state time.