Open In App

Tips for testing code in Competitive programming

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Testing Coding problems can sometimes become hectic. Here are some tips to use while testing Algorithmic Programming Problems.

There are generally four major categories of defects in program:

  1. Syntactical Errors
  2. Semantic Errors
  3. Run-time Errors / Exception
  4. Logical Errors

Syntactical Errors

Syntactical errors are generally grammatical errors in a program.

To Check the program for Syntactical errors you should follow below steps:

  • Compiling the Code in compiler
  • Some Common Syntactical errors are:
    • Misplacing Braces, parentheses etc.
    • Misplaced end of comments
    • Typographical errors
    • Misplaced Keywords

Semantic Errors 

Semantic errors may obey grammar, but violate other rules of language.
Some Common Semantic Errors are:

  • Perform incorrect operations on primitive data types.
  • Invoking instance method which is not defined.
  • Not declaring a variable before using it.

Runtime Errors / Exceptions

Runtime errors generally occur at the time of the program execution.
Examples of the runtime errors / exceptions are:

  • Division by zero
  • Array index out of bounds
  • Number of format errors
  • Null Pointer Exception

Logical Errors

Logic errors are a mistake in the design of the class or in the implementation of an algorithm.
Logical errors can be avoided by:

  • Strive for clarity and simplicity
  • Consider corner or extreme cases
  • Thinking of the pre / post conditions
  • Organizing the code

Testing Logical Errors in Code with Example

Testing Logical Errors / Bugs in code are difficult to find. Here is a walk-through of the testing logical errors in code with the help of an example.

Maximum Pairwise Product Problem – Given an array or a sequence of N numbers and goal is to find a number which can be obtained by multiplying some two numbers from this sequence.

Solutions

Naive Solution – O(N2)

Iterate through every possible pair in the sequence using a nested for loop.

C++
// Function to find the maximum
// pairwise product
int MaxPairwiseProduct(
    const std::vector<int>& numbers)
{
    int max_product = 0;
    int n = numbers.size();

    // Loop to find the pairs
    // with the maximum product
    for (int first = 0; first < n;
         ++first) {
        for (int second = first + 1;
             second < n; ++second) {
            max_product
                = std::max(
                    max_product,
                    numbers[first]
                        * numbers[second]);
        }
    }
    return max_product;
}
Java
public class Main {
    // Function to calculate the maximum pairwise product
    static int maxPairwiseProduct(int[] numbers) {
        // Initialize maxProduct to store the maximum pairwise product
        int maxProduct = 0;
        int n = numbers.length;

        // Loop to find the pairs with the maximum product
        for (int first = 0; first < n; first++) {
            for (int second = first + 1; second < n; second++) {
                // Update maxProduct if a higher product is found
                maxProduct = Math.max(maxProduct, numbers[first] * numbers[second]);
            }
        }

        // Return the maximum pairwise product
        return maxProduct;
    }

    // Main method
    public static void main(String[] args) {
        // Example usage
        int[] numbers = { 1, 2, 3, 4, 5 };
        int result = maxPairwiseProduct(numbers);
        System.out.println("Maximum Pairwise Product: " + result);
    }
}
Python3
def max_pairwise_product(numbers):
    # Initialize max_product to store the maximum pairwise product
    max_product = 0
    n = len(numbers)

    # Loop to find the pairs with the maximum product
    for first in range(n):
        for second in range(first + 1, n):
            # Update max_product if a higher product is found
            max_product = max(max_product, numbers[first] * numbers[second])

    # Return the maximum pairwise product
    return max_product
C#
using System;

class Program {
    // Function to calculate the maximum pairwise product
    static int MaxPairwiseProduct(int[] numbers)
    {
        // Initialize maxProduct to store the maximum
        // pairwise product
        int maxProduct = 0;
        int n = numbers.Length;

        // Loop to find the pairs with the maximum product
        for (int first = 0; first < n; first++) {
            for (int second = first + 1; second < n;
                 second++) {
                // Update maxProduct if a higher product is
                // found
                maxProduct = Math.Max(
                    maxProduct,
                    numbers[first] * numbers[second]);
            }
        }

        // Return the maximum pairwise product
        return maxProduct;
    }

    // Main method
    static void Main()
    {
        // Example usage
        int[] numbers = { 1, 2, 3, 4, 5 };
        int result = MaxPairwiseProduct(numbers);
        Console.WriteLine("Maximum Pairwise Product: "
                          + result);
    }
}
Javascript
// Function to calculate the maximum pairwise product
function maxPairwiseProduct(numbers) {
    // Initialize maxProduct to store the maximum pairwise product
    let maxProduct = 0;
    let n = numbers.length;

    // Loop to find the pairs with the maximum product
    for (let first = 0; first < n; first++) {
        for (let second = first + 1; second < n; second++) {
            // Update maxProduct if a higher product is found
            maxProduct = Math.max(maxProduct, numbers[first] * numbers[second]);
        }
    }

    // Return the maximum pairwise product
    return maxProduct;
}

// Main method
function main() {
    // Example usage
    let numbers = [1, 2, 3, 4, 5];
    let result = maxPairwiseProduct(numbers);
    console.log("Maximum Pairwise Product: " + result);
}

// Call the main method
main();

Efficient Approach:

The best solution for this problem would be to find the two largest and two smallest numbers from sequence and return a maximum of products between (largest, second largest) and (smallest, second smallest). And that would be in O(n) time.

Below is the implementation of the above approach:

C++
#include <iostream>
#include <vector>

// Efficient Approach for the maximum product pair
long MaxPairwiseProductFast(std::vector<int> numbers)
{
    int large1 = -1;
    int large2 = -1;
    int n = numbers.size();

    // Loop to find the index of the largest number
    for (int i = 0; i < n; ++i)
    {
        if (large1 == -1 || numbers[large1] < numbers[i])
            large1 = i;
    }

    // Loop to find the index of the second largest number
    for (int j = 0; j < n; ++j)
    {
        if (j != large1 && (large2 == -1 || numbers[large2] < numbers[j]))
            large2 = j;
    }

    // Calculate and return the product of the two largest numbers
    return (long)numbers[large1] * numbers[large2];
}

int main()
{
    // Example usage
    std::vector<int> numbers = { 1, 3, 5, 2, 7 };
    long result = MaxPairwiseProductFast(numbers);
    std::cout << "Maximum Pairwise Product: " << result << std::endl;

    return 0;
}
Java
import java.util.ArrayList;

public class Main {
    // Efficient Approach for the maximum product pair
    static long
    maxPairwiseProductFast(ArrayList<Integer> numbers)
    {
        int large1 = -1;
        int large2 = -1;
        int n = numbers.size();

        // Loop to find the index of the largest number
        for (int i = 0; i < n; ++i) {
            if (large1 == -1
                || numbers.get(large1) < numbers.get(i))
                large1 = i;
        }

        // Loop to find the index of the second largest
        // number
        for (int j = 0; j < n; ++j) {
            if (j != large1
                && (large2 == -1
                    || numbers.get(large2)
                           < numbers.get(j)))
                large2 = j;
        }

        // Calculate and return the product of the two
        // largest numbers
        return (long)numbers.get(large1)
            * numbers.get(large2);
    }

    public static void main(String[] args)
    {
        // Example usage
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(5);
        numbers.add(2);
        numbers.add(7);
        long result = maxPairwiseProductFast(numbers);
        System.out.println("Maximum Pairwise Product: "
                           + result);
    }
}
Python3
# Efficient approach for the maximum product pair
def max_pairwise_product_fast(numbers):
    large1 = -1
    large2 = -1
    n = len(numbers)

    # Loop to find the index of the largest number
    for i in range(n):
        if large1 == -1 or numbers[large1] < numbers[i]:
            large1 = i

    # Loop to find the index of the second largest number
    for j in range(n):
        if j != large1 and (large2 == -1 or numbers[large2] < numbers[j]):
            large2 = j

    # Calculate and return the product of the two largest numbers
    return numbers[large1] * numbers[large2]

def main():
    # Example usage
    numbers = [1, 3, 5, 2, 7]
    result = max_pairwise_product_fast(numbers)
    print("Maximum Pairwise Product:", result)

if __name__ == "__main__":
    main()
C#
using System;
using System.Collections.Generic;

public class MainClass
{
    // Efficient Approach for the maximum product pair
    public static long MaxPairwiseProductFast(List<int> numbers)
    {
        int large1 = -1;
        int large2 = -1;
        int n = numbers.Count;

        // Loop to find the index of the largest number
        for (int i = 0; i < n; ++i)
        {
            if (large1 == -1 || numbers[large1] < numbers[i])
                large1 = i;
        }

        // Loop to find the index of the second largest number
        for (int j = 0; j < n; ++j)
        {
            if (j != large1 && (large2 == -1 || numbers[large2] < numbers[j]))
                large2 = j;
        }

        // Calculate and return the product of the two largest numbers
        return (long)numbers[large1] * numbers[large2];
    }

    public static void Main(string[] args)
    {
        // Example usage
        List<int> numbers = new List<int> { 1, 3, 5, 2, 7 };
        long result = MaxPairwiseProductFast(numbers);
        Console.WriteLine("Maximum Pairwise Product: " + result); 
    }
}
JavaScript
// Efficient Approach for the maximum product pair
function maxPairwiseProductFast(numbers) {
    let large1 = -1;
    let large2 = -1;
    const n = numbers.length;

    // Loop to find the index of the largest number
    for (let i = 0; i < n; ++i) {
        if (large1 === -1 || numbers[large1] < numbers[i]) {
            large1 = i;
        }
    }

    // Loop to find the index of the second largest number
    for (let j = 0; j < n; ++j) {
        if (j !== large1 && (large2 === -1 || numbers[large2] < numbers[j])) {
            large2 = j;
        }
    }

    // Calculate and return the product of the two largest numbers
    return numbers[large1] * numbers[large2];
}

// Example usage
const numbers = [1, 3, 5, 2, 7];
const result = maxPairwiseProductFast(numbers);
console.log("Maximum Pairwise Product: " + result);

Output
Maximum Pairwise Product: 35

 
Testing


You can do a variety of testing on your code, but most common testing can be this three :

  1. Before submitting the code for evaluation one should test the code with example tests from the problem statement, which are the easiest to type in and verify.
  2. Small corner cases, which can be typed in by hand and for which you know the answers. We have tackled some corner cases in our implementation also: (long long) for tackling integer range issues, (if statements) and (abs) checks.
  3. Big random tests, which are generated by simple scripts to test time complexity. This type of testing is called as Stress testing.

Stress Test your Code:

This is actually a pretty standard situation when solving algorithmic programming problems. So what is stress testing? In general, it is a script that creates random tests in an infinite loop, and for each test, it calls your solution on this test and an alternative correct solution on the same test and compares the outputs. If you find a test on which your solutions output differs, you can check which one of them returns the wrong answer, debug it and then rerun the stress testing script.

Below is the example of stress testing of code of above example problem:

C++
#include <iostream>
#include <vector>
using namespace std;

long long MaxPairwiseProduct(const vector<int>& numbers) {
    int n = numbers.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)numbers[i] * numbers[j]);
        }
    }
    return max_product;
}

long long MaxPairwiseProductFast(const vector<int>& numbers) {
    int n = numbers.size();
    int max_index1 = -1;
    for (int i = 0; i < n; ++i) {
        if (max_index1 == -1 || numbers[i] > numbers[max_index1]) {
            max_index1 = i;
        }
    }
    int max_index2 = -1;
    for (int j = 0; j < n; ++j) {
        if (j != max_index1 && (max_index2 == -1 || numbers[j] > numbers[max_index2])) {
            max_index2 = j;
        }
    }
    return (long long)numbers[max_index1] * numbers[max_index2];
}

// Function to show the stress
// Testing of the code
int main()
{
    while (true) {
        int n = rand() % 3 + 2;
        cout << n << endl;
        std::vector<int> a;
        for (int i = 0; i < n; ++i) {
            a.push_back(rand() % 10);
        }
        for (int i = 0; i < n; ++i) {
            cout << a[i] << ' ';
        }
        cout << endl;
        long long res1 = MaxPairwiseProduct(a);
        long long res2 = MaxPairwiseProductFast(a);
        if (res1 != res2) {
            cout << "Wrong Answer: "
                 << res1 << ' '
                 << res2 << endl;
            break;
        }
        else
            cout << "OK\n";
    }
    return 0;
}
Java
import java.util.*;

public class Main {
    // Function to calculate the maximum pairwise product
    public static long maxPairwiseProduct(ArrayList<Integer> a) {
        int n = a.size();
        long maxProduct = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                long product = (long) a.get(i) * a.get(j);
                maxProduct = Math.max(maxProduct, product);
            }
        }
        return maxProduct;
    }

    // Function to calculate the maximum pairwise product efficiently (yet to be implemented)
    public static long maxPairwiseProductFast(ArrayList<Integer> a) {
        // Implement your efficient algorithm here
        return 0;
    }

    // Function to show the stress
    // Testing of the code
    public static void main(String[] args) {
        Random rand = new Random();
        while (true) {
            int n = rand.nextInt(3) + 2;
            System.out.println(n);
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; ++i) {
                a.add(rand.nextInt(10));
            }
            for (int i = 0; i < n; ++i) {
                System.out.print(a.get(i) + " ");
            }
            System.out.println();
            long res1 = maxPairwiseProduct(a);
            long res2 = maxPairwiseProductFast(a); // Method MaxPairwiseProductFast needs to be implemented
            if (res1 != res2) {
                System.out.println("Wrong Answer: " + res1 + " " + res2);
                break;
            } else {
                System.out.println("OK");
            }
        }
    }
}
//this code is contributed by Adarsh

 
This simple script is your stress test algorithm. Running this you can find that the previous implementations differ on some cases when duplicate elements appear in the array.


Now this is because of an overlooked mistake in the algorithm, i.e We check that number at position j is different from the maximum we’ve already found. But that’s exactly the problem. What we need is instead, that j must be different from large1 because we don’t want to find the same index, but we can find number which is equal to the first found maximum. So, instead of this comparison, what we actually need is to compare j with large1.

Final Solution without Errors:

C++
// Function to find the maximum
// pair wise product
long long MaxPairwiseProductSuperFast(
    const std::vector<int>& numbers)
{
    int poslarge1 = INT_MIN,
        poslarge2 = INT_MIN;
    int neglarge1 = INT_MIN,
        neglarge2 = INT_MIN;
    int n = numbers.size();

    if (n < 2) {
        return 0;
    }
    if (n == 2) {
        return ((long long)numbers[0]
                * numbers[1]);
    }

    // Loop to iterate over the elements
    // of the array and find the two
    // largest and two smallest elements
    for (int i = 0; i < n; ++i) {
        if (numbers[i] > poslarge1) {
            poslarge2 = poslarge1;
            poslarge1 = numbers[i];
        }
        else if (numbers[i] > poslarge2)
            poslarge2 = numbers[i];
        if (numbers[i] < 0
            && abs(numbers[i]) > abs(neglarge1)) {
            neglarge2 = neglarge1;
            neglarge1 = numbers[i];
        }
        else if (numbers[i] < 0
                 && abs(numbers[i]) > abs(neglarge2))
            neglarge2 = numbers[i];
    }

    return (
        std::max(
            (long long)poslarge1 * poslarge2,
            (long long)neglarge1 * neglarge2));
}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads