Java / Programs
1.
Write a Java program to find the last repeating character in a String.
2.
Write a Java program to find the first repeating character in a String.
3.
Write a simple Java program to find the sum of all integers in a List.
4.
Write a Java program to find the subArray in an Integer Array that has maximum Sum of the elements.
5.
Java program to find the first non-repeating character in a String.
6.
Java program to find the last non-repeating character in a String.
7.
Write a Java program to calculate Fibonacci of N using memoize recursive method.
8.
Write a Java program to count negative numbers in a 2-dimensional array which is sorted row and column wise. (Amazon interview question)
Could not find what you were looking for? send
us the question and we would be happy to answer your question.
1. Write a Java program to find the last repeating character in a String.
The below program is implemented using Java 8.
public class LastOccuranceOfRepeatingChar { public static void main(String[] args) { String str = "abcdklja"; Set<Integer> myTempHashSet = new HashSet<>(); str.codePoints().filter(i -> myTempHashSet.add(i) == false).reduce((first, second) -> second) .ifPresent(c -> System.out.println("Last repeating char: " + (char) c)); } }
The below version is for Java 7 or less.
public class LastOccuranceOfRepeatingCharJava7 { public static void main(String[] args) { String str = "abcdklja"; Set<Character> myTempHashSet = new HashSet<>(); char lastChar = 0; for (int i = 0; i < str.length(); i++) { if (myTempHashSet.add(str.charAt(i)) == false) { lastChar = str.charAt(i); } } System.out.println("Last repeating char: " + lastChar); } }
2. Write a Java program to find the first repeating character in a String.
The below program is implemented using Java 8.
public class FirstRepeatingChar { public static void main(String[] args) { String str = "abcdklha"; Set<Integer> myTempHashSet = new HashSet<>(); str.codePoints().filter(i -> myTempHashSet.add(i) == false).findFirst() .ifPresent(i -> System.out.println("First repeating Char: " + (char) i)); } }
Java7 version.
public class FirstRepeatingCharJava7 { public static void main(String[] args) { String str = "abcdklha"; Set<Character> myTempHashSet = new HashSet<>(); for (int i = 0; i < str.length(); i++) { if (myTempHashSet.add(str.charAt(i)) == false) { System.out.println("First repeating character: " + str.charAt(i)); break; } } } }
3. Write a simple Java program to find the sum of all integers in a List.
public class SumofIntegersOfList { public static void main(String[] args) { List<Integer> myList = Arrays.asList(1, 2, 3, 4, 5); System.out.println("Sum :" + myList.stream().reduce((i, j) -> i + j)); } }
4. Write a Java program to find the subArray in an Integer Array that has maximum Sum of the elements.
public class FindSubArrayWithMaxSum { public static void main(String[] args) { // Creating a bucket of integer array with various test values for unit testing int[][] myArr = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, -11 }, { 1, 2, 3, 0, 4, 3, -1, 3, 5, 5, 6, 6, -2, 50 }, { -3, 0, -1 }, { 0, -1 }, {}, { -2 }, { 2 }, { 0 }, { 1, 2, 3, -1, 1, 2, 3, 4, 0, -1, 1, 1 } }; // For each of the above data set find the sumArray with Maximum sum IntStream.range(0, myArr.length).mapToObj(i -> myArr[i]).parallel() .forEach(FindSubArrayWithMaxSum::findMaxSumSubArray); } static void findMaxSumSubArray(int[] myArr) { if (myArr.length == 0) return; int positionI = 0, positionJ = 0; int tempPositionI = 0, tempPositionJ = 0; int maxSum, tempSum; maxSum = tempSum = myArr[0]; for (int i = 1; i < myArr.length; i++) { if (tempPositionI >= i || tempSum < 0) { tempPositionI = tempPositionJ = i; tempSum = myArr[i]; continue; } if ((tempSum + myArr[i]) >= tempSum) { tempSum += myArr[i]; tempPositionJ = i; } else if (i + 1 < myArr.length) { positionI = tempPositionI; positionJ = tempPositionJ; maxSum = tempSum; tempPositionI = i + 1; } } if (tempSum > maxSum) { positionI = tempPositionI; positionJ = tempPositionJ; maxSum = tempSum; } System.out.println( Arrays.toString(myArr) + " >> Start Position: " + positionI + " End Position: " + positionJ + ", Sum: " + maxSum); } }
5. Java program to find the first non-repeating character in a String.
public class FirstNonRepeatingChar { public static void main(String[] args) { String str = "abccak"; HashMap<Character,Boolean> myLinkedMap = new LinkedHashMap<>(); for (int i=0; i<str.length(); i++) { char c = str.charAt(i); if (myLinkedMap.containsKey(c)) { myLinkedMap.put(c, false); } else { myLinkedMap.put(c,true); } } System.out.println ("First non-repeating char: " + myLinkedMap.entrySet().stream().filter(map ->map.getValue() == true).findFirst().map (p -> p.getKey())); } }
6. Java program to find the last non-repeating character in a String.
public class LastNonRepeatingChar { public static void main(String[] args) { String str = "abccak"; HashMap<Character,Boolean> myLinkedMap = new LinkedHashMap<>(); for (int i=0; i<str.length(); i++) { char c = str.charAt(i); if (myLinkedMap.containsKey(c)) { myLinkedMap.put(c, false); } else { myLinkedMap.put(c,true); } } System.out.println ("Last non-repeating char: " + myLinkedMap.entrySet().stream().filter(map ->map.getValue() == true).reduce((first,second) ->second).map (p -> p.getKey())); } }
7. Write a Java program to calculate Fibonacci of N using memoize recursive method.
public class Fibonacci { static int n; private int[] memoise_array = new int[n]; public static void main(String[] args) { n = 6; System.out.println(new Fibonacci().fibonacci(n)); } int fibonacci(int n) { if (memoise_array[n - 1] != 0) return memoise_array[n - 1]; else if (n == 1 || n == 2) return 1; else memoise_array[n - 1] = fibonacci(n - 1) + fibonacci(n - 2); return memoise_array[n - 1]; } }
8. Write a Java program to count negative numbers in a 2-dimensional array which is sorted row and column wise. (Amazon interview question)
public class CountNegativeNumbers { /* * Amazon interview question */ public static void main(String[] args) { int[][] matrix = new int[][] { { -3, -2, -1, 0 }, { -2, -1, 0, 1 }, { -1, 0, 1, 2 }, { -1, 1, 2, 3 } }; int count = 0; int k = matrix[0].length - 1; for (int i = 0; i < matrix.length; i++) { for (int j = k; j >= 0; j--) { if (matrix[i][j] < 0) { count = count + j + 1; k = j; break; } } } System.out.println("Total negative nos: " + count); } }