Missing the Performance Requirement

Topics Covered

Why Assistant Code Is Often Too Slow

Why assistants reach for brute force

Why visible tests do not catch it

What interviewers look for

An example in Python

An example in TypeScript

An example in Java

The shape of the problem

A drill for spotting slow code

Performance and the twist

Performance is not only time

Constant factors, briefly

What not to do

Thinking out loud about performance

Summary

Assistant code is often correct and too slow. It returns the right answer on every input, but on inputs as large as the problem allows, it takes far longer than the round's time limit. Visible tests are almost always small, so slow code passes them instantly. Hidden checks often include at least one large input, because performance at the stated limit is part of the problem. A solution that times out there fails, however correct it is on small inputs.

This lesson covers how to read the performance requirement from a problem, how to estimate the complexity of a diff quickly, how to confirm it with a quick measurement and how to fix code that is too slow without rewriting everything.

Brute force is fast on small tests and fails at the constraint size.

Why assistants reach for brute force

The simplest correct solution to most problems is a brute force one: try every pair, every window, every subset. It is easy to write, easy to verify and often what the assistant produces first, especially if the prompt does not mention input size. Brute force is not wrong, and for small inputs it is often the best choice. It becomes wrong when the constraints say the input can be large.

Assistants also produce code that looks efficient but is not, because the cost is hidden inside a call: a membership check on a list inside a loop, a slice inside a loop, a sum of a slice inside two loops. Each line looks cheap. The total is not.

Finally, assistants sometimes produce an efficient algorithm with one slow step, such as a sort inside a loop or a copy of a large structure on every iteration. The overall approach is right, and one detail ruins the complexity.

Why visible tests do not catch it

Visible tests in interview codebases are small for good reasons. They need to run quickly and be easy to read. A test with one hundred thousand elements is neither. As a result, the visible suite cannot distinguish a linear solution from a cubic one. Both finish in milliseconds on ten elements.

This means you cannot rely on tests to tell you about performance. You have to read the constraints, estimate the complexity yourself and, when in doubt, measure.

What interviewers look for

Interviewers are not usually looking for clever optimisations. They want to see that the candidate reads the constraints, notices when a solution will not meet them and either fixes it or explains the trade off. A candidate who says this is quadratic, the limit is one hundred thousand, so it will not finish in time; here is a linear approach shows exactly the judgement the round tests. A candidate who accepts a cubic solution because the tests pass shows the opposite.

An example in Python

The rule: return True if any two distinct elements of a list sum to the target; the list can have up to two hundred thousand elements.

diff
1+def has_pair(nums, target):
2+    for i in range(len(nums)):
3+        for j in range(i + 1, len(nums)):
4+            if nums[i] + nums[j] == target:
5+                return True
6+    return False

Correct and quadratic. On two hundred thousand elements with no matching pair, it performs about twenty billion additions, which takes far too long. A set of values seen so far, checking whether target minus the current value is in the set, makes it linear.

An example in TypeScript

The rule: return the values that appear in both arrays, in the order they appear in the first; each array can have up to one hundred thousand elements.

diff
+function common(a: number[], b: number[]): number[] {
+  return a.filter(x => b.includes(x));
+}

One line, readable and quadratic, because includes walks the whole of b for every element of a. Building a Set from b first makes each membership check constant time, and the whole function linear.

An example in Java

The rule: given a list of words, return them joined with commas; the list can have up to one hundred thousand words.

diff
1+static String joinWords(List<String> words) {
2+    String out = "";
3+    for (int i = 0; i < words.size(); i++) {
4+        if (i > 0) out += ",";
5+        out += words.get(i);
6+    }
7+    return out;
8+}

Each concatenation builds a new string by copying the old one, so the total work grows with the square of the output length. String join, or a StringBuilder, is linear.

The shape of the problem

In all three examples, the code was short, correct on small input and too slow at the stated size. The cost was visible in each one, but only if you were looking for it: two nested loops, a linear call inside a loop and repeated copying. The rest of the lesson shows how to look for it routinely.

A drill for spotting slow code

Take five problems with stated constraints. Ask an assistant to solve each without mentioning the constraints. For each solution, before running anything, estimate its complexity and decide whether it meets the constraints. Then time it at the maximum size. Note where your estimate was wrong and why. After a few rounds, your estimates will become quick and reliable.

Performance and the twist

The twist often changes the size of the problem: many queries instead of one, a stream instead of a list, updates mixed with reads. A solution that was fast enough for the original problem may not be for the twisted one. When the twist arrives, re read the constraints and re do the budget. A linear scan per query that was fine for one query is quadratic for a hundred thousand of them. Spotting this early saves you from building the twist on an approach that cannot scale.

Performance is not only time

Memory matters too, although less often in interviews. A solution that stores every pair, every substring or every intermediate state can run out of memory before it runs out of time. If the constraints are large and the solution builds a structure whose size grows with the square of n, that is as much a problem as a quadratic loop. The same counting approach works: estimate how many items each structure holds at the limit.

Constant factors, briefly

Within a complexity class, constant factors usually do not matter in interviews. A linear solution that does three passes is fine where a one pass solution would be. The exception is when the language is slow and the budget is tight, for example a Python solution at a few million operations per test. In that case, avoiding unnecessary passes or object allocations can help. But this is a late concern; get the complexity class right first.

What not to do

Do not prematurely optimise code whose constraints are small. If n is at most a hundred, the simplest correct solution is the best one, and spending time on a clever approach wastes the round. Do not replace readable code with obscure tricks for small speedups. And do not claim a complexity you have not checked; interviewers notice when a stated linear solution clearly has a nested loop.

Thinking out loud about performance

Briefly, at the moment you read the assistant's solution: this is correct; it has a loop inside a loop over n; the constraint is a hundred thousand; that will not pass. That one breath of narration tells the interviewer you have looked at performance, which many candidates do not.

Summary

Assistant code is often correct and too slow: brute force, hidden linear calls or one expensive step inside a good approach. Visible tests are too small to show it. Read the constraints, estimate the cost and say what you find.

Most of the time, a few seconds of reading is all it takes to decide whether a solution will meet its limit.

Correct is not enough at the limit

A solution that gives the right answer on small inputs and times out at the stated limit fails the problem. Visible tests are almost always too small to show this, so read the constraints and check the complexity yourself.