Tips for testing code in Competitive programming
Last Updated :
01 Oct, 2024
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:
- Syntactical Errors
- Semantic Errors
- Run-time Errors / Exception
- 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);
}
}
Python
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);
}
}
Python
# 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);
OutputMaximum Pairwise Product: 35
Testing
You can do a variety of testing on your code, but most common testing can be this three :
- 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.
- 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.
- 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
Python
import random
# Function to calculate the maximum pairwise product
def max_pairwise_product(a):
n = len(a)
max_product = 0
for i in range(n):
for j in range(i + 1, n):
product = a[i] * a[j]
max_product = max(max_product, product)
return max_product
# Function to calculate the maximum pairwise product efficiently (yet to be implemented)
def max_pairwise_product_fast(a):
# Implement your efficient algorithm here
return 0
# Function to show the stress testing of the code
def stress_testing():
while True:
n = random.randint(3, 4) # Choose the range for n, here (3, 4)
print(n)
# Generate random numbers for array a
a = [random.randint(0, 9) for _ in range(n)]
print(*a) # Print the elements of array a
res1 = max_pairwise_product(a)
# Method max_pairwise_product_fast needs to be implemented
res2 = max_pairwise_product_fast(a)
if res1 != res2:
print("Wrong Answer:", res1, res2)
break
else:
print("OK")
# Run the stress testing function
stress_testing()
# this code is contributed by Prachi.
JavaScript
// Function to calculate the maximum pairwise product
function maxPairwiseProduct(a) {
let n = a.length;
let maxProduct = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let product = a[i] * a[j];
maxProduct = Math.max(maxProduct, product);
}
}
return maxProduct;
}
// Function to calculate the maximum pairwise product efficiently (yet to be implemented)
function maxPairwiseProductFast(a) {
// Implement your efficient algorithm here
return 0;
}
// Function to show the stress testing of the code
function stressTesting() {
while (true) {
let n = Math.floor(Math.random() * (4 - 3 + 1)) + 3; // Choose the range for n,
// here (3, 4)
console.log(n);
let a = [];
for (let i = 0; i < n; i++) {
a.push(Math.floor(Math.random() * 10)); // Generate random numbers for array a
}
console.log(...a); // Print the elements of array a
let res1 = maxPairwiseProduct(a);
let res2 = maxPairwiseProductFast(a); // Method maxPairwiseProductFast need
//to be implemented
if (res1 !== res2) {
console.log("Wrong Answer:", res1, res2);
break;
} else {
console.log("OK");
}
}
}
// Run the stress testing function
stressTesting();
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++
#include <climits> // For INT_MIN
#include <cmath> // For abs
#include <iostream>
#include <vector>
long long
MaxPairwiseProductSuperFast(const std::vector<int>& numbers)
{
int posLarge1 = INT_MIN;
int posLarge2 = INT_MIN;
int negLarge1 = INT_MIN;
int negLarge2 = INT_MIN;
int n = numbers.size();
// Check for edge cases
if (n < 2) {
return 0;
}
else if (n == 2) {
return ((long long)numbers[0] * numbers[1]);
}
// Loop to find the two largest and two smallest
// elements
for (int num : numbers) {
if (num > posLarge1) {
posLarge2 = posLarge1;
posLarge1 = num;
}
else if (num > posLarge2) {
posLarge2 = num;
}
if (num < 0
&& std::abs(num) > std::abs(negLarge1)) {
negLarge2 = negLarge1;
negLarge1 = num;
}
else if (num < 0
&& std::abs(num) > std::abs(negLarge2)) {
negLarge2 = num;
}
}
// Return the maximum pairwise product
return std::max((long long)posLarge1 * posLarge2,
(long long)negLarge1 * negLarge2);
}
int main()
{
std::vector<int> numbers = { 1, 2, 3, -4, -5 };
std::cout << MaxPairwiseProductSuperFast(numbers)
<< std::endl; // Output: 20
return 0;
}
Java
import java.util.Arrays;
public class MaxPairwiseProductSuperFast {
public static int
maxPairwiseProductSuperFast(int[] numbers)
{
// Initialize variables to store the two largest
// positive numbers
int posLarge1 = Integer.MIN_VALUE;
int posLarge2 = Integer.MIN_VALUE;
// Initialize variables to store the two largest
// negative numbers
int negLarge1 = Integer.MIN_VALUE;
int negLarge2 = Integer.MIN_VALUE;
int n = numbers.length;
// Check for edge cases
if (n < 2) {
return 0;
}
else if (n == 2) {
return numbers[0] * numbers[1];
}
// Loop to find the two largest and two smallest
// elements
for (int num : numbers) {
if (num > posLarge1) {
posLarge2 = posLarge1;
posLarge1 = num;
}
else if (num > posLarge2) {
posLarge2 = num;
}
if (num < 0
&& Math.abs(num) > Math.abs(negLarge1)) {
negLarge2 = negLarge1;
negLarge1 = num;
}
else if (num < 0
&& Math.abs(num)
> Math.abs(negLarge2)) {
negLarge2 = num;
}
}
// Return the maximum pairwise product
return Math.max(posLarge1 * posLarge2,
negLarge1 * negLarge2);
}
public static void main(String[] args)
{
int[] numbers = { 1, 2, 3, -4, -5 };
System.out.println(maxPairwiseProductSuperFast(
numbers)); // Output: 20
}
}
// This code is contributed by Kishan
Python
def max_pairwise_product_super_fast(numbers):
# Initialize variables to store the two largest positive numbers
pos_large1 = float('-inf')
pos_large2 = float('-inf')
# Initialize variables to store the two smallest negative numbers
neg_small1 = float('inf')
neg_small2 = float('inf')
n = len(numbers)
# Check for edge cases
if n < 2:
return 0
elif n == 2:
return numbers[0] * numbers[1]
# Loop to find the two largest positive numbers and two smallest negative numbers
for num in numbers:
if num > pos_large1:
pos_large2 = pos_large1
pos_large1 = num
elif num > pos_large2:
pos_large2 = num
if num < neg_small1:
neg_small2 = neg_small1
neg_small1 = num
elif num < neg_small2:
neg_small2 = num
# Calculate the maximum pairwise product
max_product_positive = pos_large1 * pos_large2
max_product_mixed = neg_small1 * neg_small2
# Return the maximum of the two products
return max(max_product_positive, max_product_mixed)
# Example usage
numbers = [1, 2, 3, -4, -5]
print(max_pairwise_product_super_fast(numbers)) # Output: 20
JavaScript
function MaxPairwiseProductSuperFast(numbers) {
let posLarge1 = -Infinity;
let posLarge2 = -Infinity;
let negLarge1 = Infinity;
let negLarge2 = Infinity;
const n = numbers.length;
// Check for edge cases
if (n < 2) {
return 0;
} else if (n === 2) {
return numbers[0] * numbers[1];
}
// Loop to find the two largest and two smallest elements
for (let num of numbers) {
if (num > posLarge1) {
posLarge2 = posLarge1;
posLarge1 = num;
} else if (num > posLarge2) {
posLarge2 = num;
}
if (num < negLarge1) {
negLarge2 = negLarge1;
negLarge1 = num;
} else if (num < negLarge2) {
negLarge2 = num;
}
}
// Return the maximum pairwise product
return Math.max(posLarge1 * posLarge2, negLarge1 * negLarge2);
}
// Example usage
const numbers = [1, 2, 3, -4, -5];
console.log(MaxPairwiseProductSuperFast(numbers)); // Output: 20
//This code is contributed by Aman.