You know what O(n log n) means. You have solved the problem on LeetCode twice. Then the interviewer asks "what's the complexity of that?" and your mind goes blank, or you mumble "linear, I think" and hope nobody asks why.

Freezing here is rarely a knowledge problem. It is a narration problem: you have never practised turning the analysis into sentences while someone watches. This post is a set of small, repeatable scripts for doing exactly that.

Why interviewers care so much about the narration

In most coding rounds the final code is only part of the score. The interviewer is also writing down whether you:

  • noticed the cost of your approach before you were asked,
  • could compare two approaches in terms of time and memory,
  • found the inputs that break your code before running it.

A correct solution delivered in silence often scores lower than a slightly rough one delivered with clear reasoning. The good news: the reasoning can be practised like a script.

Time complexity: count the loops, then name the dominant term

A reliable way to stay calm is to analyse in the same order every time, out loud:

  1. Name the input sizes. "Let n be the length of the array and k the window size."
  2. Walk the code top to bottom. For each loop, say how many times it runs and what each iteration costs.
  3. Add sequential parts, multiply nested parts.
  4. Drop the lower-order terms and say the result.

Here is how that sounds on a real function:

def has_pair_with_sum(nums, target): seen = set() for x in nums: # runs n times if target - x in seen: # average O(1) set lookup return True seen.add(x) # average O(1) insert return False 

"One pass over n elements. Inside the loop I do a set lookup and a set insert, both O(1) on average. So the whole thing is O(n) time on average. Worst case for a hash set is worse, but that's not the case I'd expect here."

Notice the phrase on average. Saying it unprompted is a small signal that you understand hashing rather than reciting a number.

Sentences worth memorising

  • "The loop runs n times and each iteration is constant work, so O(n)."
  • "The outer loop is n, the inner loop shrinks each time, so it's n + (n−1) + … which is O(n²)."
  • "I sort first, which is O(n log n), and the scan after it is O(n), so sorting dominates: O(n log n)."
  • "Each recursive call halves the input and does constant work, so O(log n)."

If you are unsure, say the bound you can justify: "It's at most O(n²); I think it's tighter, let me check." Thinking aloud beats a confident wrong answer.

Space complexity: separate the input, the output and the extra

Space is where candidates freeze most, because "what counts?" is genuinely ambiguous. Remove the ambiguity by splitting it into three parts and saying which one you mean:

  • Input space: the array you were given. Usually not counted.
  • Output space: the list you return. Say whether you count it.
  • Auxiliary space: everything else you allocate, including the call stack.

"Not counting the input, I use a hash set that can grow to n entries, so O(n) extra space. If memory mattered, I could sort in place and use two pointers: O(1) extra space, but O(n log n) time and I'd be modifying the input."

That last sentence is the one interviewers love: a trade-off, stated plainly, with its cost.

The call stack trips people up in recursive solutions. A recursive DFS on a tree uses O(h) stack space, where h is the height: O(log n) for a balanced tree, O(n) for a tree that is basically a linked list. Say both.

More worked examples, including the recursion-stack cases, are in this post on explaining space complexity out loud.

Edge cases: use a fixed checklist, not inspiration

"What edge cases can you think of?" feels like a creativity test. It isn't. Run the same list every time and you will find the important ones:

Category Ask yourself
Empty Empty array, empty string, null / None
Single One element, one character, one node
Duplicates All equal values, repeated keys
Boundaries First / last index, k = 0, k = n, k > n
Signs and size Negative numbers, zero, integer overflow
Shape Sorted, reverse sorted, a skewed tree, a cycle in a graph

Say them before you write the code, then point back to them after:

"Before I start: empty input returns False. A single element can't form a pair. Duplicates matter — if target is 10 and the array has one 5, that's not a pair, and my set version handles it because I check before inserting."

Then, after coding, trace one edge case by hand. One clean trace is worth more than listing ten cases you never test. The edge cases guide on the AI Interview Assistant blog has a longer list per data structure.

A 30-second template for the whole explanation

When you finish a solution, deliver this in one breath:

  1. Approach: "I used a hash set to remember what I've seen."
  2. Time: "One pass, O(n) on average."
  3. Space: "O(n) extra for the set."
  4. Edge cases: "Empty and single-element inputs fall through the loop and return False; duplicates are handled by checking before inserting."
  5. Alternative: "If memory were tight, sort plus two pointers gives O(1) extra space at O(n log n) time."

Five sentences. Practise them on every problem you solve this week, even alone, and they become automatic.

How to practise so it sticks

  • Say it aloud even when solving alone. Typing the analysis in a comment is not the same skill.
  • Record one session a week and listen for the moments you go quiet. Those are the gaps to drill.
  • Swap roles with a friend: one solves, the other only asks "why?" after every claim.
  • Keep a one-line note per pattern (sliding window, BFS, heap) with its usual time and space. Review the notes, not the solutions.

A tool that helps in the live round

Practice gets you most of the way. In the real interview, though, the blank moment can still happen. We built AI Interview Assistant for that hour. It is a desktop app that follows the call audio and, for a coding question, puts the likely approach, its time and space cost and the edge cases to mention in a compact panel beside your editor. It is a prompt to jog your memory, not a script, and it has a free plan if you want to try it in a mock interview first.

What is the complexity question that has caught you out in an interview? Drop it in the comments and let's talk it through.