Hi codeforces! If you stuck on a problem that you are sure your solution is correct, but in somehow it gets WA, and then you opened the tutorial, so your solution is the same, but you don't know what is the bug, what do you do for this situation? I tried AI agents but most of them stuck also. thx already :)








Auto comment: topic has been updated by Ahmad_OS (previous revision, new revision, compare).
Think very hard. Or stress test.
Some useful things to do are creating many assertions throughout your code (to ensure your assumptions are correct along with ensuring no UB is occurring), running your code with sanitization flags, running your code with and without optimization flags, and finding a reproducible break case. Taking a break then logically stepping through your code can also help.
I stare at the code until the bug gets embarrassed and leaves.
Depends on the circumstances honestly. If I'm solving outside of a live contest (which I assume is what you are referring to), I usually write a brute force to find a reasonably-sized testcase where my code has incorrect output. Then I debug my code with breakpoints (you can do this with gdb via terminal or using your code editor's native debugging tools) and compare the variable values to what they are supposed to be. This solves ~60-70% of my problems, and if you write a clean enough code then it's not a problem to navigate through it, even if it's pretty long. This tactic also works on OI-style contests, and it's something that I used at IOI to debug a 400 line solution within reasonable time.
I second this. If you can find a case that you solve wrong, either by trying a few by hand (works surprisingly well) or with a stresstesting script, stepping through your logic can really help.
If you don't have a case, like today when I got WA on D (it was because I looped until log(n) instead of log(max a_i)) and that problem is not stresstestable, I tend to just try to read the code carefully and try to find the problem. Unfortunately, this is not very fun and doesn't always work lol
If even the AI is getting stuck, you have to accept the truth. Ask for help from the god
by using a debuggerby putting a bunch of print statements and dry running the code to catch the place where it doesn't behave as intended
I have met problems like that many times.In my perspective,the best way to solve them is to rewrite the code.
send it to the community group and beg for help :)
1 ) Stress test to find a test that has a different answer from the correct solution.
2 ) After you find a test , try to find why is your solution giving that wrong answer (read it word by word) , maybe that would help a bit.
we don't debug, we are the bug xD
When I am doing a problem that is at least partly constructive, I usually try to use the test cases that are provided to "generate" even more test cases to test my code on.
For example, today I practised on the problem "Frequencies" from JBOI 2023. I used the first test case of the example tests and wrote down the arrangements after each step of the construction. I then then tried each of these steps and used it to work out exactly where my error occurred.
It can be harder to make your own test cases for other types of question, but it is still usually the best "first stop" for debugging. However, it can also be good to carefully trace though the algorithm on paper.
If all else fails, you could try to explain your algorithm to someone else. This is helpful as often-times when you talk aloud about the problem, it can help you with spotting errors.
Stress test
I guess, I just wanted to fully explain what I meant.
I hope it somehow pops up in my brain
What agents do you use? Maybe that's the key point.
I tried many free AI agents because there is no payment method in Syria, but actually I found that deepseek with thinking mode was better than chat gpt at debugging.
I set my compiler to dark theme because bugs love light.
first by adding many print statements , if not solve then
find the test case on which my code fails (by printing it on codeforces)
On Codeforces, you can usually print the wrong testcase you are failing on.
Codeforces usually shows the printed output telling it as the wrong output, so you can use this to print your array as a string, like:
python print('@'.join(map(str, arr)))and print it only when that particular testcase (say the 64th) fails.
python if totaltestcasescount == x and t == 64: print('@'.join(map(str, arr)))Instead of debugging later, while writing each thing, separate the things you are doing. Suppose you are doing a divide and do it as
x,y,zseparate things. Use functions. It becomes much easier to verify your assumptions.Even if somehow things went wrong, think if some condition might fail for a group of tests. Sometimes you solve it for that specific failing testcase say you put extra condition or solve it seperatly and leave it, but it might actually be because of some group of testcases like that. So instead of fixing just that testcase, try to generalize it.
If nothing like the above exists, then once you find a failing testcase, trace it. Print the values and compare them with your assumed values. Since you have the editorial or a similar implementation, compare the prints. If both blocks are supposed to be doing the same thing, you'll usually find where they first differ.
If you couldn't find any failing testcase:
If you don't have the editorial, then come from the first, separate your code into blocks and ask:
These questions will usually find the bug much faster than changing
itoi+1and hoping it ACs.by debugging it.