Блог пользователя AmeyPatil

Автор AmeyPatil, история, 5 месяцев назад, По-английски

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

  • Проголосовать: нравится
  • -9
  • Проголосовать: не нравится

»
5 месяцев назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится
»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, # ^ |
      Проголосовать: нравится +12 Проголосовать: не нравится

    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 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        5 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Brute force probably

      • »
        »
        »
        »
        5 месяцев назад, # ^ |
          Проголосовать: нравится +2 Проголосовать: не нравится

        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 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You can use CF Stress. For example, Ticket 17446

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Think about edge cases as well