Python debugging practice

Each solution below has a bug or three. Run the examples, fix the code, then submit it against the hidden tests.

Loading a puzzle…

Constraints

    Code

    Loading a puzzle…

    Python 3.12 in your browser

    Why practice debugging?

    Most of programming is reading code that almost works. Writing a solution from scratch trains one skill; finding the line that’s quietly wrong trains another: reading carefully, forming a guess, and testing it. Short, focused puzzles let you do that in a minute or two.

    Every puzzle here starts from a correct reference solution with one to three small changes made to it, the kind of slips that pass a quick glance: < where it should be <=, the wrong variable, a starting value off by one, a line indented into the loop.

    How to work through one

    Read the problem and the examples first, then the code, and predict what it returns for one example before you press Run. When the output differs, the gap usually points at the bug. Hard puzzles hide one bug from the examples, so think about edge cases: an empty list, one element, negatives, duplicates.

    Stuck? The first hint marks the lines with bugs. The second shows the fix, which is a good way to learn a pattern you keep missing.

    Questions

    How does it check my fix?

    Each problem has 3 examples you can run as often as you like and 20 more hidden tests. Submit runs all 23 and stops at the first one that fails, showing its input, your output and the expected answer. Any fix that passes every test counts, not just the original line.

    What kinds of bugs are there?

    Twelve kinds you meet in real code: off-by-one boundaries, flipped comparisons, wrong operators, swapped variables, bad starting values, broken accumulators, slice and index mistakes, wrong boolean logic, a missing return, a missing guard or break, and lines at the wrong indentation.

    How are the levels different?

    Easy has one bug that an example catches. Medium has one or two bugs, each caught by an example. Hard has two or three, and at least one only shows up in the hidden tests, so you have to think about edge cases.

    Where does my code run?

    In your browser, with Pyodide (Python 3.12 compiled to WebAssembly). Python downloads once on your first Run, then stays cached. Nothing you type is sent anywhere.

    What if my code loops forever?

    Runs stop after five seconds and show Time Limit Exceeded; Python restarts in the background so you can keep going.

    Are these LeetCode problems?

    No. They’re 100 original problems in the style of easy interview questions, each with a reference solution checked against its tests and a second, independent implementation.