Open In App
Related Articles

Importance of Testing In Competitive Programming

Improve Article
Save Article
Like Article

Many times while solving problems, we face issues like time limit exceeds, wrong solution, runtime error, and memory limit exceeds because after designing algorithm we don’t test algorithm efficiency, correctness, time complexity and running the algorithm on a large set of inputs for testing purpose. 
This is where stress testing comes for rescue. Stress testing is the common way to find the bug in the algorithm. 
which involves generating a large set of random test cases and checking if a brute force algorithm and an efficient algorithm always agree with each other. Many programmers are not familiar with this kind of comprehensive testing. 
stress testing helps in finding an efficient algorithm and correct algorithms to problems but also see why certain approaches do not work. In particular, it is easy to sketch intuitive greedy solutions to any problems, but such solutions often do not work in reality. 
Write a Generator program which output some random input. 
Step’s Involved In Stress Testing 
– Generate a random input. 
– Run your solution on it. 
– Check if the output is correct. 
– if not-stop and output the test cases.
Generate small tests for faster and easier debugging especially for trivial solution. make parameters to easily tweak test size. Do not loose generality and check for special cases. 
1 Stress test after manual test. 
2 No Point if generator/trivial solution / checker is too complex 
3 Start with very small test sizes. 
4 Couple of minutes running testing program is usually enough. 
5 if bug is not found, generate larger tests.


// Find a Maximum pairwise product in array of Integer.
#include <bits/stdc++.h>
using namespace std;
// trivial solution
long long MaxPairwiseProductslow(const std::vector<int>& v)
    int n = v.size();
    long long max_product = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            max_product = max(max_product, (long long)(v[i]) * (v[j]));
    return max_product;
// efficient solution
long long MaxPairwiseProduct(const std::vector<int>& v)
    sort(v.begin(), v.end());
    int n = v.size();
    long long max_product = 0;
    max_product = ((long long)v[n - 2]) * v[n - 1];
    return max_product;
int main()
    // random generator
    while (true) {
        int n = rand() % 10 + 2;
        cout << n << " \n";
        vector<int> a;
        for (int i = 0; i < n; i++) {
            a.push_back(rand() % 100000);
        for (int i = 0; i < n; i++) {
            cout << a[i] << " ";
        cout << "\n";
        long long res1 = MaxPairwiseProduct(a); // result of efficient solution
        long long res2 = MaxPairwiseProductslow(a); // // result of trivial solution
        if (res1 != res2) // when two outputs dont match
            cout << "Wrong answer: " << res1 << res2 << "\n";
        else {
            cout << "Success"
                 << "\n";
    int n;
    std::cin >> n;
    std::vector<int> v(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> v[i];
    long long result = MaxPairwiseProduct(v);
    std::cout << result << "\n";
    return 0;

Input Example: 
1 2 3
Output: 6
Input Example:
7 5 6 1 3 7 10

Summary Test your solution before to avoid errors. Start testing with given samples. check for corner cases. special/hidden test cases. Manual cases like min-max, problem-type specific and anything you could imagine. test different part separately. If everything else fails, run stress-test

Last Updated : 09 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials