Learn
Your loop said no after looking at only the first item
The function is six lines long, both samples pass, and you are already talking about complexity. Then the interviewer gives you a list where the interesting value sits second, and the answer comes back wrong. Nothing in the loop is complicated. One line returns too early.
The situation
A warehouse logs temperature readings from a cold room. You are asked whether any reading went over a limit. It is a warm-up question, and that is exactly why the bug survives: nobody slows down for it.
def any_over(readings, limit):
for r in readings:
if r > limit:
return True
else:
return False
It reads well aloud: "if it is over, yes, otherwise no". Try [9, 2] with a limit of 5. The first reading is over, so it returns True. Correct. Try [1, 2]. The first reading is under, so it returns False. Also correct. Two samples, two greens.
The input that exposes it
[1, 9] with a limit of 5. The first reading is under the limit, the else branch returns False, and the loop never reaches the 9. The function has answered a question about the whole list after looking at one item.
The smallest breaking input has two readings, the first under and the second over. Any sample where the first item happens to decide the answer cannot catch it. Samples are often written that way, because the person writing them puts the interesting case first.
There is a second, quieter failure. With an empty list the loop never runs, and the Python version falls off the end and returns None. In an if that behaves like False, so it looks right, until someone compares the result with is False or serialises it.
The hidden assumption is the first item speaks for the rest. It is true only for an answer that one item can prove.
The rule, and its mirror image
Ask what a single item can prove.
- For "is there any reading over the limit", one reading over the limit proves yes. No single reading can prove no; only the whole list can.
- For "are all invoices paid", one unpaid invoice proves no. Only the whole list can prove yes.
So the early return is allowed only for the answer one item can prove. The other answer belongs after the loop:
def any_over(readings, limit):
for r in readings:
if r > limit:
return True # one witness is enough
return False # only now have we seen every reading
And the mirror image in Java, where the wrong version tends to return true on the first paid invoice:
static boolean allPaid(int[] owed) {
for (int amount : owed) {
if (amount != 0) {
return false; // one counterexample is enough
}
}
return true; // only now have we seen every invoice
}
Now [1, 9] returns True, [1, 2] returns False, and the empty list returns False in Python and true in Java, which is the honest answer for "are all of none paid".
A Python detail worth knowing: move that else: four spaces to the left, so it lines up with for, and the original function becomes correct. A loop's else runs only when the loop finishes without leaving early. Four spaces decide between a bug and a correct answer, which is a good reason to say the rule aloud rather than trust the shape.
The same mistake has an accumulator form. A learner writing up their third week of practice described a return res indented inside the loop, so a running maximum was returned after the first round. Same cause: an answer about all the items, given after one.
What to say out loud
Before the loop, name the question and its proof: "This is an any question. One reading over the limit proves yes, so I can stop there. I can only say no after the loop."
Then name the input that would catch you: "If I got the early exit wrong, a list with the decisive value last would show it, so I will trace [1, 9]." That second sentence is what turns a warm-up into evidence that you check your own code.
The exercise
- Write
any_overin Python andallPaidin Java from memory, without looking at the versions above. - For each, write down which answer a single item can prove. Put that answer inside the loop and the other one after it.
- Trace three inputs by hand before running anything: the decisive item first, the decisive item last, and an empty list.
- Write the broken
else-return version on purpose and find the smallest input that tells the two apart. - Take one running-total function you wrote recently and check that its
returnsits outside the loop.
Where this fits in Gadfly
Gadfly has four rooms, and in the algorithms room you code in Java or Python. In tutor mode it asks the next question rather than revealing the solution, so "which answer can one item prove?" is the kind of question it puts to you when an early return looks too early. "I'm stuck" gives one more step, up to pseudocode, never finished code. Gadfly records what went wrong in a session and uses it when choosing what you practise next.
Published 21 November 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].