Dry run: the first day an account is overdrawn

Interview craft Junior Python 3.13+ Reviewed By Best Latest Apps

Predict the output

What does this print?

Python 3.13
balance, out = 3, []
for day, change in enumerate([-1, -1, 4, -7, 2]):
    balance += change
    out.append((day, balance))
print(out)

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A [(0, 2), (1, 1), (2, 5), (3, -2), (4, 0)]

Right: the balance first goes below zero on day 3.

Writing the running value after each step is exactly the dry-run table, and it shows the answer for the shown solution is day 3.

Why not the others?
  • B [(0, 2), (1, 1), (2, 5), (3, -2)]. This loop records every day; it does not stop at the first negative.
  • C [(1, 2), (2, 1), (3, 5), (4, -2), (5, 0)]. enumerate starts at 0.
A line-by-line trace of the overdrawn-day function, showing day, change and balance after each executed line, for an account that goes negative on day 3 and one that never does.

Traced by running the Python solution.

Text version

Start at 3 (args [[-1, -1, 4, -7, 2], 3]):

Trace: Start at 3
Linedaychangebalance
2
33 (changed)
40 (changed)-1 (changed)3
50-12 (changed)
30-12
41 (changed)-12
51-11 (changed)
31-11
42 (changed)4 (changed)1
5245 (changed)
3245
43 (changed)-7 (changed)5
53-7-2 (changed)
63-7-2

14 rows, result 3.

Start at 1, never negative (args [[1, -1, 2], 1]):

Trace: Start at 1, never negative
Linedaychangebalance
2
31 (changed)
40 (changed)1 (changed)1
5012 (changed)
3012
41 (changed)-1 (changed)2
51-11 (changed)
31-11
42 (changed)2 (changed)1
5223 (changed)
3223
7223

12 rows, result -1.

30-second answer

One pass with a running balance: add each change and return the day it first goes negative. O(n) time, O(1) memory. The point of this page is the dry run: walking the code by hand on a small input catches off-by-one errors and wrong return values before the interviewer does.

Practise saying it · 30 s. No recording, just a timer

Explain how you dry-run your code.

Say your answer out loud, then check which of these points you covered:

  • Small, interesting input
  • Variables after each step
  • Normal and edge case

Deep dive

Pick a dry-run input that exercises the interesting branch (here, a day that goes negative).

Write variables in a small table: one row per step.

Also dry-run an input where the branch never happens, to check the final return.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def first_overdrawn_day(changes: list[int], start: int) -> int:
    balance = start
    for day, change in enumerate(changes):
        balance += change
        if balance < 0:
            return day
    return -1

Common mistakes

  • Dry-running in your head without writing anything down.
  • Choosing an input so simple that no branch is exercised.

Senior take & follow-ups

Announce 'let me dry-run this on [example]' before the interviewer asks.

Follow-up questions

Think of your answer first, then open the hint.

  1. How many dry runs should you do?

    Show hint

    Usually two: one normal case and one edge case.

  2. What if the dry run finds a bug?

    Show hint

    Say so, fix it, and rerun the same input.

Suggest a fix if something here is wrong or out of date.