Learn
Your fast solution agrees with every example you thought of
You write the clever version, try it on two examples you made up, and both come out right. Then the interviewer offers one more input, and the answer is off by one. Your examples were the ones your solution was built around, so they could never disagree with it.
The weakness
When you test your own fast solution, you pick inputs from the same picture of the problem that produced the code, so a blind spot in the code is a blind spot in your examples too. And a hand trace tells you what the code does, not what it should do.
The fix is a second opinion you trust: a slow version so simple that it is obviously correct. At home you let a loop compare the two on many small random inputs. In the room you use the slow version to get the expected answer for a small input before you check the fast one. This week builds both habits.
What goes wrong, on one small input
An original task: meetings are given as (start, end) pairs, and a meeting occupies a room from start up to, but not including, end. Return the largest number of meetings running at the same moment. There is at least one meeting.
def peak_slow(meetings):
# a meeting (s, e) is running at time t when s <= t < e
return max(sum(1 for s, e in meetings if s <= t < e)
for t, _ in meetings)
def peak_fast(meetings):
events = []
for s, e in meetings:
events.append((s, 0)) # 0 marks a start
events.append((e, 1)) # 1 marks an end
events.sort()
now = best = 0
for _, kind in events:
now += 1 if kind == 0 else -1
best = max(best, now)
return best
The slow version checks every start time, because a peak always begins when some meeting starts. It is quadratic and hard to get wrong. On [(1, 4), (2, 5), (6, 8)] both return 2.
Now let a loop do the choosing:
import random
for trial in range(2000):
meetings = []
for _ in range(random.randint(1, 4)):
s = random.randint(0, 5)
meetings.append((s, s + random.randint(1, 3)))
if peak_fast(meetings) != peak_slow(meetings):
print(meetings, peak_fast(meetings), peak_slow(meetings))
break
Within a few trials it prints a short list where the fast version says one more than the slow one. Shrink it by hand and you reach [(1, 3), (3, 5)]: one meeting ends exactly when the next starts. The tuple (3, 0) sorts before (3, 1), so the start is counted before the end, and for a moment two meetings appear to overlap. The fix is to mark a start as +1 and an end as -1, so that at equal times the end sorts first, and to add the mark to now directly. After that change, twenty thousand random inputs agree.
With times from 0 to 5, equal times turn up constantly. Small ranges are the point.
What to say out loud
Before the fast version: "The simple answer is to check every start time and count the meetings running then. That's quadratic, so I won't submit it, but I'll use it to check myself." Then compute it on a tiny input that has a tie: "On one meeting from 1 to 3 and one from 3 to 5, the slow version says one, because the first has ended at 3. So my sweep has to say one as well." If the sweep says two, you have found the bug before the interviewer did.
The exercise
Seven days from tomorrow, one check a day, written down in one line.
- Monday: the slow versions. Take three fast solutions you wrote recently, in Java or Python. For each, write the slowest correct version you can think of. Check: did each one take under five minutes?
- Tuesday: the comparison loop. For one of them, write a loop like the one above. Keep sizes to one to five items and values to a range of about six. Check: did it find a disagreement, and on how many items?
- Wednesday: shrink. For every disagreement, remove items one at a time while the two versions still disagree. Write one sentence on why the smallest input breaks the fast version. Check: did the input shrink to three items or fewer?
- Thursday: the worst case, not the best. For each solution, name the input that makes its inner loop run longest, then count loop steps (not seconds) on it at two sizes. Check: did the count grow the way your stated complexity says? If not, your "worst case" may be the best case.
- Friday: the room version. Solve one new problem aloud, recording yourself on your phone. Say the slow version in one sentence first, work out its answer by hand on two tiny inputs, one with a tie or a repeat, then write the fast version and check it against those answers. Check: did you state the slow version before writing any code?
- Saturday: a timed round. Forty minutes, one new problem, the same rule as Friday. Check: did the hand-computed answers catch anything before you ran the code?
- Sunday: the review. List every disagreement the week found and give each a kind: equal values, empty or single input, negatives, ties at a boundary. Check: which kind came up most often? That is the tiny input you try first next week.
Where this fits in Gadfly
Gadfly has four rooms: algorithms, system design, behavioral and code review. In the algorithms room you code in Java or Python. Tutor mode asks the next question and never reveals the solution; saying "I'm stuck" gives one more step, up to pseudocode, never finished code. Mock mode behaves like an interviewer: no coaching during the round, and a debrief at the end quoted from the record, not a grade. Gadfly records what went wrong in a session and uses it when choosing what you practise next. The slow versions and the comparison loop in this plan need nothing but an editor.
Published 29 November 2026 · A practice plan for the week · 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].