AmeyPatil's blog

By AmeyPatil, history, 5 months ago, In English

Today while solving a problem I came up with this solution . which passes the first test case but gives wrong answer on the 2nd case and the problem is Idk what the test is since its not visible . I have tried generating random test cases by myself and also using chatgpt to understand which case it could be failing on but no luck . So i wanted to ask how do you guys debug it . this is the first time I have encountered this issue (i am new :P) . Any help on the problem would also be appreciated :) . thanks

  • Vote: I like it
  • -9
  • Vote: I do not like it

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

You don't. Looking at failed test cases and then debugging the problem is like taking hints, which is not forbidden, but it hinders your thinking ability and you'll develop a habit of it. You're supposed to figure out the failed test cases on your own.

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

    No, it's perfectly fine. Especially during contests (generate a thousand cases, one of them will be wrong) is much more reliable than coming up with them on your own.

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

      And how do you create them and check wether they are correct?

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

        Brute force probably

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

        To create: write a dumb generator that can plausibly generate any small test case. It's not important that it has uniform distribution or anything.

        To check: usually you can write a stupid brute force solution. It might run in exponential time but that should be fine: most bugs can be caught with like $$$n = 8$$$.

        Sometimes if there are multiple answers, you can instead write a small checker function in your code and add a simple assert.

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

      Well, what I said was true for myself, at least. I used to submit solutions without really having an idea of how they worked and when I got a WA, I used to quickly look up at the failed tests and sometimes they gave away the solution.

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

The best way I do it is stress testing because it is difficult for me to come up with test cases during a contest and mostly my code fails for huge integers which is not possible to debug I was also facing this issue. Check this video out

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

Stress test.

I have created a sample that will cause your program to fail.

1
5
6 1 1 8 1
1 5 8 6 5
  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thankyou so much , this test case helped me to solve the problem .

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

    The most negative-looking positive comment I saw.

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

No Doubt ! you can try various things like stress testing, generating edge cases, etc. But if you want to view the test which is failing you can use the below approach.

spoiler

Few Days back I was solving the problem 1978C - Manhattan Permutations. On first submission I got WA on test 2 266054646, so wrote added this extra code to print the input on terminal 266058305.

code

Hence I got the failed test case, and then I corrected it 266059198.

Also if you are getting a WA there are a high chances that it might come in test 2, because 3 and further are mostly designed to check if the code gives TLE.

Furthermore if an array type input is giving the wrong answer, use the same technique which I mentioned above :

code

This technique mostly works because on test 2 mostly the length of array and array values are small and we can easily seperate them by adding some 4-5 zeros bewtween them.

Hope this helps !!!

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

You can use CF Stress. For example, Ticket 17446

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

Think about edge cases as well