Code works for Sample but I get WA | Beginner

I am facing this problem while solving multiple problems.

My code works for the sample input and I get the correct output, but when I go to submit my code, it gives me a ‘wrong answer’ message.
I usually code my answers based on the sample input and output. Should I change my coding perspective?

Let me know what you think.

Sample doesn’t always give you all cases . It just shows you some cases.Relying on samples is not the way to go, but u can use them for basic checks only. If there is penalty for wrong submission, you need to think of corner cases and some other possible cases before you submit your code.

2 Likes

Can you tell me how to look out for these corner cases?

sample input and output are just for better understanding of problem, passing sample test cases does not means that your code is correct. while solving a problem you have to think on a broader prespective by considering all the possible test cases.

  • before submit try to test your code for corner test cases and make sure that you code passes all those test cases.
  • look at the constraints properly and make sure your code fits with all constraints, (if you are using languages like C++ make sure there are no overflows for large input and output)
  • before coding you algorithm try to calculate the time complexity of your algorithm so that you will not face a TLE
1 Like

There are some things that you have to check after passing the samples:

  1. Overflows. If your code has int, the range of an int is -2,147,483,648 \text{ to } 2,147,483,647. Make sure you are not computing something that is greater than this range. If it turns out to be, use long long. (This is for C++ or JAVA, there aren’t such problems in Python).
  2. Edge cases: sometimes the input can be negative. Or the answer might be different for n = 1. Or the answer might be different for all even n. There are many such corner cases, which you’ll have to think of. These might not be covered in the samples given.
1 Like