Learn
Your best-run total says zero when every single day lost money
The best total over consecutive values is one of the first one-pass tricks people learn: keep a running sum, drop it when it goes negative, remember the best. You write it in four lines, it passes the example with a few losses in the middle, and the interviewer asks what it returns when every value is negative. It returns 0, and 0 is not in the input.
The situation
A market stall records its profit or loss for each day as a list of integers. You are asked for the best total over a run of one or more consecutive days. The usual first version looks like this:
def best_run(changes):
best = cur = 0
for x in changes:
cur = max(0, cur + x)
best = max(best, cur)
return best
On [2, -5, 3, -1, 4] it returns 6, from the run 3, -1, 4, which is right. On any list with at least one positive day it is right.
The input that exposes it
The smallest breaking input is a single bad day: best_run([-1]). The stall lost 1, so the only run there is totals -1. The function returns 0. On [-3, -1, -4] it also returns 0, where the answer is -1.
The hidden assumption is that 0 is a safe starting value. It is not neutral. A total of 0 is the total of an empty run, a run of no days, and the question asked for one or more. Every time cur is reset to 0, the code is saying "start again with nothing", and best is allowed to keep that nothing as its answer.
Sometimes the empty run is allowed. If the question were "the stall may skip trading entirely; what is the best it can do?", then 0 is the correct answer for an all-loss list. That is the point: the code answers one of two questions, and you have to say which one you were asked.
The Java version has a second trap waiting for the person who fixes the first one by starting from the smallest integer:
int cur = Integer.MIN_VALUE, best = Integer.MIN_VALUE;
for (int x : changes) {
cur = Math.max(x, cur + x); // first step: MIN_VALUE + (-1)
best = Math.max(best, cur);
}
On {-1}, cur + x overflows to Integer.MAX_VALUE, so cur and best both become the largest integer there is. A sentinel that takes part in arithmetic is not a sentinel.
The invariant and the fix
Say what each variable means before you write the loop: cur is the best total of a run that ends today and includes today; best is the best total of any non-empty run seen so far. Both statements are true after the first day only if both start at the first day's value, not at 0.
def best_run(changes):
best = cur = changes[0]
for x in changes[1:]:
cur = max(x, cur + x) # extend yesterday's run, or start today
best = max(best, cur)
return best
Trace it on [-3, -1, -4]: both start at -3; on -1, cur becomes max(-1, -4) = -1 and best becomes -1; on -4, cur becomes -4 and best stays -1. On [2, -5, 3, -1, 4] it still returns 6. The Java fix is the same: initialise both from changes[0] and loop from index 1, so nothing is ever added to a sentinel.
Now the empty list matters. changes[0] raises an error on [], and that is a question for the interviewer, not something to hide behind a default.
What to say out loud
Before the first line: "Does the run have to contain at least one day, or can the stall choose to do nothing? If every day is a loss, should I return the smallest loss or 0?"
While you write the start values: "I am starting from the first value, not from 0, because 0 would be the total of an empty run."
When you test, say the case you are choosing and why: "The example has a positive day, so it cannot tell the two versions apart. I will try a list where every value is negative, and a list with one value."
The exercise
- Without running anything, write what the first version returns for
[-1],[-3, -1, -4],[0, -2]and[5]. - Write the question each answer would be correct for: "one or more days" or "zero or more days".
- Change the first version so that it answers "one or more days", and trace it by hand on all four inputs.
- In Java, write the
Integer.MIN_VALUEversion and find on paper the first value ofxthat makescur + xwrong. - Look at three loops you wrote recently that keep a best-so-far, and write next to each one whether its starting value could be returned as the answer.
Where this fits in Gadfly
Gadfly has four rooms; in the algorithms room you code in Java or Python, and its catalog of 711 problems is tagged by the pattern each one trains. In tutor mode it asks the next question and never reveals the solution, so "what does your function return when every value is negative?" is the kind of question it asks, and "I'm stuck" gives one more step, up to pseudocode, never finished code. In mock mode it behaves like an interviewer, with no coaching during the round and a debrief at the end. A problem you struggled with comes back on day 1, 3, 7 and 14, and one you found easy comes back once.
Published 5 December 2026 · One mistake, one counterexample · all notes · feed
This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].