Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 63 ยท Getting Started with Contests

Verdicts and Debugging

Understand judge verdicts WA, RE, TLE, MLE and PE, reproduce failures with small hand tests, use assertions and printed state, and hunt off-by-one errors.

Every submission ends with a verdict, and most of them, at first, are not "Accepted". The skill that separates quick improvers from stuck ones is not avoiding mistakes; it is finding them fast. A verdict is a clue about what kind of mistake you made. Combined with a few systematic habits, it narrows the search from "somewhere in 80 lines" to a short list of suspects.

This lesson goes through each common verdict, what usually causes it, and how to reproduce it locally. All examples are small and run in your browser. You met the verdict names in What Is Competitive Programming?; now we act on them.

A debugging map

flowchart TD
    V{"Verdict"} -->|"WA"| W["Logic or edge case:<br/>small tests, brute force"]
    V -->|"RE"| R["Crash: index, zero division,<br/>recursion, bad parsing"]
    V -->|"TLE"| T["Too slow:<br/>complexity, input and output, loops"]
    V -->|"MLE"| M["Too much memory:<br/>huge tables, copies, lists"]
    V -->|"PE"| P["Formatting: spaces,<br/>newlines, case"]
    W --> H["Hand-made tests"]
    R --> H
    H --> F["Fix and resubmit"]

Match the verdict to a family of causes, then look for the cheapest test that reproduces it.

Wrong Answer

Wrong Answer is the most common verdict and the least informative. Your program ran, finished in time and printed something different from what the judge expected on some test. Typical causes:

  • A misread statement (strict versus non-strict, indexing base, which answer to print).
  • An algorithm that is right on the samples but wrong in general.
  • An edge case: n = 1, all values equal, the largest possible values, negative numbers.
  • Off-by-one errors in loops and ranges.
  • Output format: printing yes when YES is required.
  • Leaving state from one test case in a global variable and carrying it into the next.

Your tools: tiny hand-made tests and a brute force to compare against. We build the second of these properly in Stress Testing with a Brute Force; here we use the idea in miniature.

Hunt with small tests

Suppose the task is "print the largest sum of any non-empty contiguous subarray". A tempting but wrong solution starts the running maximum at 0:

Python A wrong answer found by a tiny test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The wrong solution passes every test that contains a positive number, so a sample would probably pass it. Only the all-negative cases expose it: the answer must be the largest single element (-1), but the code reports 0, which corresponds to an empty subarray the statement forbids. The tiny test [-5] finds the bug in one second. Always try minimal inputs: one element, two elements, all negative, all equal.

Runtime Error

Your program crashed. In Python the crash is an uncaught exception, and the judge shows a generic RE (sometimes with the exception name). Common causes:

  • IndexError: indexing past the end, or -1 accidentally wrapping around to the last element.
  • ZeroDivisionError, ValueError from int(""), or KeyError.
  • RecursionError from deep recursion (see Python for Contests).
  • Reading more input than exists, for example forgetting that the last line may be missing.
  • A non-zero exit code from sys.exit(1) or an assert that fails.

To reproduce, run on the smallest and largest valid inputs and read the exception name and line number. Catching an exception in a local test harness gives you the same information a judge hides:

Python Reproducing a runtime error with a safe harness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each broken input produces a different exception, and the harness tells you which line raised it. Reproduce first, then think.

Time Limit Exceeded

Your answers may be right, but at least one test was too slow. Check these suspects in order:

  1. Complexity. Estimate the operations for the maximum input as in Reading a Problem Statement. A double loop over 2 * 10^5 elements will never pass.
  2. Hidden costs. x in list, list.pop(0), list.insert(0, x), slicing inside a loop, string concatenation in a loop, and sorted() called repeatedly are all O(n) each.
  3. Input and output. Thousands of print calls or slow input() can use up the time limit by themselves.
  4. Infinite loops. A while loop whose condition never becomes false on some input. Print the loop variables for a tiny case and watch them.
  5. Worst-case structures. Sorted input making a naive algorithm quadratic, or a long path graph making a search deep.

Create a maximum-size test yourself (random numbers, all equal, sorted, reversed) and time it. If the time grows about four times when you double n, you have a quadratic algorithm.

Memory Limit Exceeded

Python objects are large: a list of a million ints holds a million pointers and (for ints above 256) a million objects, so it may need tens of megabytes. Memory errors come from big 2D tables (a 5000 x 5000 table is 25 million cells), storing all strings or edges unnecessarily, and building copies of large lists. Fixes: keep only the previous DP row, use array or bytes for compact storage, avoid copying slices, and process input as a stream. We measure it in Estimating Time and Memory.

Presentation Error and format mistakes

Some judges report a separate PE; many report it as WA. Check:

  • Case: Yes versus YES.
  • Separators: spaces versus newlines.
  • Trailing characters: extra blank lines are usually fine, but extra text is not.
  • Decimal precision: the statement says how many digits or what error is allowed.

A habit that avoids the worst of these: copy the output format words straight from the statement.

Assertions and printing state

When a bug is hidden in the middle of the program, make the program check itself. An assert states a fact that must be true; if not, the program stops at the first violation, which is exactly where the bug started.

Printing state is the other tool. Print to standard error (print(..., file=sys.stderr)) so debug output never pollutes the answer; most judges ignore stderr. Remove or disable it before submitting if it is large, since heavy printing can cost time.

Python Assertions find the first broken moment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The checks encode the contract of the function: sorted inputs, no lost elements, sorted output. If a later change breaks any of them, the failing assertion names the broken promise. Assertions that cost O(n) are fine while testing; remove expensive ones in the final submission.

Off-by-one errors

The most frequent bug family. Examples:

  • range(1, n) stops at n - 1; use range(1, n + 1) for 1 through n.
  • Slices exclude the end: a[l:r] has r - l elements.
  • Binary search boundaries: hi = mid versus hi = mid - 1.
  • Prefix sums: prefix[r] - prefix[l] covers [l, r), so the inclusive version uses r + 1.
  • Counting items between positions l and r inclusive: r - l + 1.

A reliable defence is to write down the half-open interval convention [l, r) at the top of the problem and stick to it everywhere. Another is to test with the smallest sizes, where an off-by-one changes the result visibly.

Python Off-by-one: three ways to count a window

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same positions give 90 or 50 depending on whether the right end is included; both are right for their own convention, and wrong if mixed.

A debugging routine

  1. Read the verdict and, if available, the failing test number (the first failing test is usually the simplest).
  2. Reread the statement for the word you may have misread.
  3. Write the smallest possible inputs, work them out by hand, and run them.
  4. Compare with a brute force if you can.
  5. Add assertions and stderr prints; find the first moment your state differs from your expectation.
  6. Fix one thing at a time, and resubmit only when you can explain why the previous version failed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which verdict most often means 'your logic or an edge case is wrong'?

    WA means the program finished within the limits but printed the wrong output on some test, usually because of an unhandled edge case or a wrong idea.
  2. 2. A max-subarray solution starts with best = 0 and passes all samples. Which test should break it?

    With only negative numbers, the correct answer is the largest element, -1, but the code returns 0 because it allows an empty subarray.
  3. 3. Where should debug prints go so they do not corrupt the answer?

    Judges compare standard output only. Debug text on stdout is part of your answer and will cause Wrong Answer.
  4. 4. a[l:r] is a slice. How many elements does it contain?

    Slices include the left end and exclude the right end, so the length is r - l.

Summary

  • Each verdict points at a family of causes: WA at logic and edge cases, RE at crashes, TLE at complexity and hidden costs, MLE at oversized structures, PE at formatting.
  • Reproduce failures with tiny hand-made tests, including n = 1, all negatives and all equal values.
  • A brute force gives you a ground truth to compare against.
  • Use assert to stop at the first broken invariant, and print debug text to stderr.
  • Off-by-one errors are tamed by choosing one interval convention and sticking to it.
  • Debug methodically: verdict, statement, small tests, comparison, fix one thing.

Next: Templates and Habits