Ahmad_OS's blog

By Ahmad_OS, history, 5 months ago, In English

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 :)

  • Vote: I like it
  • +22
  • Vote: I do not like it

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Ahmad_OS (previous revision, new revision, compare).

»
5 months ago, hide # |
 
Vote: I like it +21 Vote: I do not like it

Think very hard. Or stress test.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

»
5 months ago, hide # |
 
Vote: I like it +32 Vote: I do not like it

I stare at the code until the bug gets embarrassed and leaves.

»
5 months ago, hide # |
← Rev. 2  
Vote: I like it +23 Vote: I do not like it

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.

  • »
    »
    5 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    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

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

If even the AI is getting stuck, you have to accept the truth. Ask for help from the god

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

by using a debugger

by putting a bunch of print statements and dry running the code to catch the place where it doesn't behave as intended

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I have met problems like that many times.In my perspective,the best way to solve them is to rewrite the code.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

send it to the community group and beg for help :)

»
5 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

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.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

we don't debug, we are the bug xD

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

»
5 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

I hope it somehow pops up in my brain

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What agents do you use? Maybe that's the key point.

  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I set my compiler to dark theme because bugs love light.

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

first by adding many print statements , if not solve then

find the test case on which my code fails (by printing it on codeforces)

»
4 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

On Codeforces, you can usually print the wrong testcase you are failing on.

Spoiler
  1. 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, z separate things. Use functions. It becomes much easier to verify your assumptions.

  2. 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.

  3. 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.

  4. 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:

    • Is this block doing what I wanted?
    • What is this variable used for?
    • What assumptions am I making?
    • Is my assumption always true?

These questions will usually find the bug much faster than changing i to i+1 and hoping it ACs.

»
4 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

by debugging it.