Assessment 3_12_sample
Instructions
This assessment is designed to be completed in 10 minutes or less.
Copy the template below and get all the test cases to pass. Hard-coding answers will not receieve credit; your solution must work for valid arrays.
The problem
Imagine the user enters two lists of integers. Return a third array, which merges the first two arrays in sorted order. For example, if the two input arrays were [1, 3, 2]
and [7, 4]
, your code would return the array [1, 7, 3, 4, 2]
.
To save time, we’ve created a result array for you that is 100 elements long, and filled with zeroes; you can simply place the kept items into this array, in order. The grading script will remove the extra zeroes at the end automatically (and you can take a look at the cleanResult
method in the template if you’re curious how it works).
Code Template and Test cases
import java.util.Arrays;
public class Assess3_12_Sample{
public static int[] merge(int[] arr1, int[] arr2){
int [] result = new int[10];
return result;
}
public static int[] cleanResult(int[] array){
return Arrays.stream(array).filter(num -> num != 0).toArray();
}
public static void main(String[] args){
int [] arr1a = {};
int [] arr1b = {};
int [] result1 = {};
System.out.println("test1: " + Arrays.equals(cleanResult(merge(arr1a, arr1b)), result1));
int [] arr2a = {1};
int [] arr2b = {};
int [] result2 = {1};
System.out.println("test2: " + Arrays.equals(cleanResult(merge(arr2a, arr2b)), result2));
int [] arr3a = {1, 3};
int [] arr3b = {2};
int [] result3 = {1, 2, 3};
System.out.println("test3: " + Arrays.equals(cleanResult(merge(arr3a, arr3b)), result3));
int [] arr4a = {2, 1, 3};
int [] arr4b = {7, 8, 6, 4, 5};
int [] result4 = {2, 7, 1, 8, 3, 6, 4, 5};
System.out.println("test4: " + Arrays.equals(cleanResult(merge(arr4a, arr4b)), result4));
int [] arr5a = {1, 2, 3, 3};
int [] arr5b = {4};
int [] result5 = {1, 4, 2, 3, 3};
System.out.println("test5: " + Arrays.equals(cleanResult(merge(arr5a, arr5b)), result5));
int [] arr6a = {};
int [] arr6b = {1, 3};
int [] result6 = {1, 3};
System.out.println("test6: " + Arrays.equals(cleanResult(merge(arr6a, arr6b)), result6));
}
}