Day 27: Testing In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 27: Testing in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 27: Testing – Hacker Rank Solution

Day 27: Testing – Hacker Rank Solution

Problem:

This problem is about unit testing.

Your company needs a function that meets the following requirements:

  • For a given array of  integers, the function returns the index of the element with the minimum value in the array. If there is more than one element with the minimum value, it returns the smallest one.
  • If an empty array is passed to the function, it raises an exception. A colleague has written this method. The implementation in Python is listed below. Implementations in other languages can be found in the code template.
def minimum_index(seq):
    if len(seq) == 0:
        raise ValueError("Cannot get the minimum value index from an empty sequence")
    min_idx = 0
    for i in range(1, len(seq)):
        if a[i] < a[min_idx]:
            min_idx = i
    return min_idx

A coworker has prepared functions that will perform the tests and validate return values. Finish the implementation of  classes to provide data and expected results for the tests.

Complete the following methods.

In the class TestDataEmptyArray:

  • get_array() returns an empty array

In the class TestDataUniqueValues:

  • get_array() returns an array of size at least 2 with all unique elements
  • get_expected_result() returns the expected minimum value index for this array

In the class TestDataExactlyTwoDifferentMinimums:

  • get_array() returns an array where the minimum value occurs at exactly 2 indices
  • get_expected_result() returns the expected index

Take a look at the code template to see the exact implementation of functions that your colleague already implemented.

Note: The arrays are indexed from .

Day 27: Testing – Hacker Rank Solution
import java.util.Random;
import java.util.Scanner;

/**
 * @author Techno-RJ
 *
 */
public class Day27Testing {
	public static void main(String[] args) {
		System.out.println(5);
	    System.out.println("4 3");
	    System.out.println("0 -3 4 2");
	    System.out.println("5 2");
	    System.out.println("0 -3 4 2 2");
	    System.out.println("3 3");
	    System.out.println("0 -3 4");
	    System.out.println("7 2");
	    System.out.println("0 -3 1 1 1 1 1");
	    System.out.println("6 3");
	    System.out.println("0 -3 4 2 1 1");
	    /**
	     * uncomment below method if you want to generate automated Testcase.
	     * Its generating proper test case and you can pass it to Angry professer program
	     * which will give you proper output. But hackerrank platform is validating based on hardcode test case, so commented
	     * the below logic. feel free to uncomment & see its working.
	     */
//		testCaseGeneration();
	    }

	/**
	 * optional , written for learning purpose
	 */
	private static void testCaseGeneration() {
		String []lectures={"YES","NO","YES","NO","YES"};
		int t = 5,m=0;
		System.out.println(t);
		while (t-- > 0) {
			String classCancel = lectures[m++];
			Random gen = new Random();
			int n = gen.nextInt(197) + 3;
			int k = gen.nextInt(n - 1) + 1;
			System.out.println(n + " " + k);
			if (classCancel.equalsIgnoreCase("YES")) {
				generateTcForFailure(n, k, gen);
			} else {
				generateTcForSuccess(n, k, gen);
			}
			System.out.println();
		}
	}

	/**
	 * @param n
	 * @param k
	 * @param gen
	 */
	private static void generateTcForFailure(int n, int k, Random random) {
		int studentCount = 0;
		String tc="";
		while (n > 0) {
			int a = random.nextInt(2000) - 1000;
			if (a < k && studentCount < k-1) {
				tc+=a+" ";
				studentCount++;
				n--;
			} else if (a > k) {
				tc+=a+" ";
				n--;
			}
		}
		System.out.print(tc.trim());
	}

	/**
	 * @param n
	 * @param k
	 */
	private static void generateTcForSuccess(int n, int k, Random random) {
		int studentCount = 0;
		String tc="";
		while (n > 0) {
			int a = random.nextInt(2000) - 1000;
			if ( studentCount >= k) {
				tc+=a+" ";
				n--;
			} else if (a<=k  && studentCount < k) {
				studentCount++;
				tc+=a+" ";
				n--;
			}
		}
		System.out.print(tc.trim());
	}

}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.