Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do programmers test their algorithm in TopCoder or other competitions?

Good programmers who write programs of moderate to higher difficulty in competitions of TopCoder or ACM ICPC, have to ensure the correctness of their algorithm before submission.

Although they are provided with some sample test cases to ensure the correct output, but how does it guarantees that program will behave correctly? They can write some test cases of their own but it won't be possible in all cases to know the correct answer through manual calculation. How do they do it?

Update: As it seems, it is not quite possible to analyze and guarantee the outcome of an algorithm given tight constraints of a competitive environment. However, if there are any manual, more common traits which are adopted while solving such problems - should be enough to answer the question. Something like best practices..

like image 283
Vivek Rai Avatar asked Jul 11 '13 09:07

Vivek Rai


2 Answers

In competitions, the top programmers have enough experience to read the question, and think of some test cases that should catch most of the possibilities for input.
It catches most of the bugs usually - but it is NOT 100% safe.

However, in real life critical applications (critical systems on air planes or nuclear reactors for example) there are methods to PROVE some piece of code does what it is supposed to do.

This is the field of formal verification - which is way too complex and time consuming to be done during a contest, but for some systems it is used because mistakes could not be tolerated.


Some additional information:
Formal verification basically consists of 2 parts:

  1. Manual verification - in here we use proving systems such as Hoare logic and manually prove the program does what we wants it to do.
  2. Automatic model checking - modeling the problem as state machine, and use Model Checking tools to verify that the module does what it is supposed to do (or not doing something "bad").
    Specifying "what it should do" is usually done with temporal logic.
    This is often used to verify correctness of hardware models as well. For example Intel uses it to ensure they won't get the floating point bug again.
like image 169
amit Avatar answered Nov 19 '22 20:11

amit


Picture this, imagine you are a top programmer.Meaning you know a bunch of algorithms and wouldn't think think twice while implementing them.You know how to modify an already known algorithm to suit the problem's needs.You are strong with estimating time and complexity and you expect that in the worst case your tailored algorithm would run within time and memory constraints.
At this level you simply think and use a scratchpad for about five to ten minutes and have a super clear algorithm before you start to code.Once you finish coding, you hit compile and there is usually no compilation error.Because the code is so intuitive to you. Then based on the algorithm used and data structures used, you expect that there might be one of the following issues.

  1. a corner case
  2. an overflow problem

A corner case is basically like you have coded for the general case, however when say N=1, the answer is different from others.So you generally write it as a special case. An overflow is when intermediate values or results overflow a data type's limits.

You make note of any problems which arise at this point, and use this data during Challenge phase(as in TopCoder).
Once you have checked against these two, you hit Submit.

like image 40
Aravind Avatar answered Nov 19 '22 18:11

Aravind