Skip to main content

Debugging & Stress Testing

The Cost of Debugging in CP

Every minute spent debugging is a minute not solving the next problem. Learn to debug fast.
Contest Scoring Systems on Codeforces:ICPC Rules (most Div 2/3/4 contests):
  • Each wrong submission adds +10 minute penalty
  • Final score = time of AC + (10 × wrong attempts)
  • Earlier solve = better rank
IOI Rules (Educational rounds, some special contests):
  • Partial scoring based on test cases passed
  • No time penalty for wrong submissions
  • Score = points earned (not time-based)
  • Maximum points per problem (e.g., 100, 250, 500, etc.)
Codeforces Scoring (standard Div 1/2 rounds):
  • Points decrease over time (max points at start)
  • Each wrong submission costs -50 points (before AC)
  • No penalty after AC

Prevention > Debugging

Before You Code

1

Understand the Problem Completely

Re-read after you think you understand. Check examples manually.
2

Plan Your Approach

Think through edge cases before coding.
3

Choose Data Types Carefully

Will values overflow int? Use long long proactively.
4

Write Clean Code

Use meaningful variable names. Keep functions small.

Common Bug Prevention


Systematic Debugging Process

Step 1: Read the Verdict

Step 2: Test Edge Cases

Always test these inputs:

Step 3: Trace Through by Hand

For small inputs, trace your code step by step:

Step 4: Add Debug Output


Common Bug Patterns

Integer Overflow

Off-by-One Errors

Uninitialized Variables

Array Index Errors

Multiple Test Case Issues

Floating Point Comparison


Debug Macros Template

Add this to your template:
Compile with -DLOCAL to enable debug output:

Stress Testing

What is Stress Testing?

Stress testing = Running your solution against a brute force on random inputs until they disagree.

Complete Stress Test Setup

File 1: solution.cpp (your optimized code)
File 2: brute.cpp (simple correct solution)
File 3: gen.cpp (random test generator)
File 4: stress.bat (Windows)
File 4: stress.sh (Linux/Mac)

Quick Debug Checklist

When you get WA, check in this order:
1

Did I read the problem correctly?

Re-read problem statement. Check constraints again.
2

Integer overflow?

Use long long for any multiplication or large sums.
3

Edge cases?

Test n=1, n=0, all same, all different.
4

Off-by-one?

Check loop bounds, array indexing.
5

Uninitialized variables?

Check all variables are initialized.
6

Multiple test cases?

Are you resetting everything between cases?
7

Output format?

Spaces, newlines, YES vs Yes vs yes?

Debugging Different Verdicts

Wrong Answer (WA)

  1. Test all examples manually
  2. Create small custom test
  3. Check edge cases
  4. Add debug output
  5. Stress test if needed

Time Limit Exceeded (TLE)

  1. Check complexity analysis
  2. Look for accidental O(n²) operations
  3. Check for infinite loops
  4. Use faster I/O if not already
  5. Consider algorithm change

Runtime Error (RE)

  1. Check array bounds
  2. Check division by zero
  3. Check stack overflow (deep recursion)
  4. Check null pointer / empty container

Memory Limit Exceeded (MLE)

  1. Check array sizes
  2. Avoid unnecessary copies
  3. Use iterative instead of recursive
  4. Use memory-efficient data structures

Pro Debugging Tips

When stuck on one test case:

Binary Search on Test Cases

If failing on test case 50 out of 100:

Assert for Assumptions


Key Takeaways

Prevention First

Use safe defaults: long long, initialize variables, check bounds.

Systematic Process

Follow a checklist. Don’t randomly try fixes.

Debug Macros

Have debug macros ready in your template.

Stress Test

When stuck, stress test against brute force.

Next Steps

Environment Setup

Set up VS Code with tasks for stress testing.