More Data Structure and Algorithms Coding Questions and answers in Java.
Q1. Write a program that allows you to create an integer array of 5 elements with the following values: int numbers[ ]={5,2,4,3,1}. The program computes the sum of first 5 elements and stores them at element 6, computes the average and stores it at element 7 and identifies the smallest value from the array and stores it at element 8.
A1. Remember that the arrays are zero based.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Numbers { public static void main(String args[ ]) { // last three zeros are for the result int numbers[ ] = { 5, 2, 4, 3, 1, 0, 0, 0 }; //set the minimum to a max value numbers[7] = Integer.MAX_VALUE; for (int i = 0; i < 5; i++) { // sum the numbers numbers[5] += numbers[i]; // track the lowest if (numbers[i] < numbers[7]) { numbers[7] = numbers[i]; } } // average the numbers numbers[6] = numbers[5] / 5; System.out.println("Total is " + numbers[5]); System.out.println("Average is " + numbers[6]); System.out.println("Minimum is " + numbers[7]); } } |
Output:
1 2 3 4 | Total is 15 Average is 3 Minimum is 1 |
Q2. Other than looping through each element in an array and setting each one to null, is there a better way to do it?
A2. The Arrays and Collections utility classes in Java has very handy methods.
1 | Arrays.fill(myArray, null); |
Q3. Can you complete the following method so that
1 2 3 | public int[] withoutTen(int[] nums) { } |
An input of {10, 2, 10, 1} yields {2, 1, 0, 0}. The 10’s to be removed, and the remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be filled with 0’s.
A3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class RemoveTens { public static void main(String[] args) { int[] input = {10, 2, 10} ; int[] withoutTen = new RemoveTens().withoutTen(input); System.out.println(Arrays.toString(withoutTen)); } public int[] withoutTen(int[] nums) { List<Integer> without10 = new ArrayList<Integer>(); //store numbers that are not ten in order for (int i = 0; i < nums.length; i++) { if(nums[i] != 10){ without10.add(nums[i]); } } //assign not tens first and then tens towards the end for (int i = 0; i < nums.length; i++) { if(i <= (without10.size()-1)){ nums[i] = without10.get(i); } else { nums[i] = 0; } } return nums; } } |
Q4. Can you complete the following method so that
1 2 3 | public boolean has99(int[] nums) { } |
Given an array of numbers, return true if the array contains two 9’s next to each other, or there are two 9’s separated by one element.
{ 2, 9, 2, 2, 9, 9 } –> false
{ 2, 9, 2, 9, 9 } –> true
A4.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class NineNine { public static void main(String[] args) { int[] input = { 2, 9, 2, 2, 9, 9 }; boolean result = new NineNine().has99(input); System.out.println(result); } public boolean has99(int[] nums) { boolean found = false; //decide if the default is false or true int numToCompare = 9; for (int i = 0; i < nums.length; i++) { if(nums[i] == numToCompare){ //care should be taken to avoid ArrayIndexOutOfBoundsException exception if((i+1 < nums.length && nums[i+1] == numToCompare) || (i+2 < nums.length && nums[i+2] == numToCompare)){ found = true; break; //no need to continue once found } } } return found; } } |
Q5 Given a number n, create and return a new string array of length n, containing the strings “0”,”1″ “2” .. through n-1. n may be 0, For example, if n = 4, returns {“0”, “1”, “2”, “3”}
A5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Arrays; public class StringArray { public static void main(String[] args) { String[] strArray = new StringArray().numbersAsArray(4); System.out.println(Arrays.toString(strArray)); } public String[] numbersAsArray(int n) { if(n == 0){ return new String[]{}; //return an empty array } String[] result = new String[n]; for (int i = 0; i < n; i++) { result[i] = String.valueOf(i); } return result; } } |