Step 01 Beginner Problems — Fundamentals of Programming
Dry Runs, Edge Cases and Debugging
Professional programmers do not stare at code hoping to spot the bug — they dry run it. A dry run means executing your own code on paper, one line at a time, tracking every variable in a table. Combined with a habit of testing edge cases first, it turns debugging from panic into a procedure. This lesson gives you that procedure, plus the catalogue of hostile inputs that break beginner programs.
The trace-table method
Draw columns for each variable and one row per step. Run the code with a small input and write down every change. Example — summing the digits of 405 with the loop 'while n > 0: s += n % 10; n //= 10':
step | n | digit | s start| 405 | - | 0 1 | 40 | 5 | 5 2 | 4 | 0 | 5 3 | 0 | 4 | 9
The answer 9 is correct — and notice the trace also proves the loop terminates, because n shrinks every iteration. If a trace column ever stops changing while the loop continues, you have found an infinite loop before the computer did.
The edge-case catalogue
Run every solution against this checklist before trusting it:
Zero and one — loops that never run, and formulas that divide by the input. Negative numbers — digit loops and remainders behave differently; does the problem even allow them? Empty and single-element inputs — arrays and strings of length 0 and 1 break more solutions than any other case. All duplicates / already sorted / reverse sorted — inputs with no variety expose hidden assumptions. The maximum constraint — does the answer overflow a 32-bit integer? Does an O(n^2) loop survive n = 100000?
In interviews, naming the edge cases you tested is itself a strong positive signal.
A calm debugging workflow
Step 1: Reproduce. Find the smallest input that fails and keep it — never debug against a huge input. Step 2: Verify understanding. Dry run that input and find the first step where reality departs from your expectation; the bug lives at or before that step, never after it. Step 3: Form one hypothesis ('the loop stops one iteration early') and test only that. Step 4: Fix the root cause, then re-run the failing case and your earlier passing cases.
What not to do: change random lines hoping the answer improves. Every unexplained change destroys information about where the bug was.
The five bugs you will meet repeatedly
Off-by-one: loop runs one time too few or too many — trace the first and last iteration specifically. Uninitialised accumulator: sum or count starts from garbage instead of 0. Destroyed input: you consumed n in a loop and then needed the original — copy it first. Wrong division: in many languages 5 / 2 is 2, not 2.5 — use integer division deliberately. Condition copied wrong: < instead of <=, which only shows up on boundary inputs — exactly the ones in the edge-case catalogue.
Key points
- A dry run is a paper execution: one row per step, one column per variable — it proves correctness and termination.
- If a variable stops changing inside a loop, you have an infinite loop; if a trace and code disagree, trust the trace.
- Test the catalogue: 0, 1, negatives, empty, single element, all duplicates, sorted/reverse-sorted, maximum size.
- Debug by procedure: shrink the failing input, find the first wrong step, test one hypothesis, fix the root cause.
- The recurring five: off-by-one, uninitialised accumulator, destroyed input, wrong division, flipped comparison.