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
- 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.)
- 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
| Verdict | First Action |
|---|---|
| WA | Check edge cases, trace smallest example |
| TLE | Check complexity, look for infinite loops |
| RE | Check bounds, division by zero |
| MLE | Check array sizes, recursive depth |
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:-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)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)
- Test all examples manually
- Create small custom test
- Check edge cases
- Add debug output
- Stress test if needed
Time Limit Exceeded (TLE)
- Check complexity analysis
- Look for accidental O(n²) operations
- Check for infinite loops
- Use faster I/O if not already
- Consider algorithm change
Runtime Error (RE)
- Check array bounds
- Check division by zero
- Check stack overflow (deep recursion)
- Check null pointer / empty container
Memory Limit Exceeded (MLE)
- Check array sizes
- Avoid unnecessary copies
- Use iterative instead of recursive
- Use memory-efficient data structures
Pro Debugging Tips
Print and Exit
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.