BeginnerHabits & complexity
Dry-run your code on paper
Trace your code line by line on a tiny input, writing every variable down. It finds bugs faster than re-reading.
The idea
Re-reading code mostly confirms what you meant to write. Tracing it on a small input shows what it actually does. Write a table with one column per variable and update it line by line.
Pick an input of 3–5 elements that hits your main loop and at least one special case.
Worked example
Tracing a buggy “count positives” loop on [1, -2, 3]
- Code: count = 0; for i in range(1, len(nums)): if nums[i] > 0: count += 1
- i=1: nums[1] = -2 → count stays 0
- i=2: nums[2] = 3 → count = 1
- Answer 1, expected 2. The trace shows i never took 0: the range should start at 0.
How to spot it
- Before you submit, and whenever the result surprises you.
Common mistakes
- Tracing in your head instead of on paper; you'll skip the line with the bug.
- Choosing an input so simple it never enters the loop.
Practise it
Go further
Up next for you · Beginner
“Have I seen this?” is a set →