Java 8 coding interview questions and solutions.

Ramasarma
2 min readJun 10, 2024
  1. Count total occurrence each char in a string.
pubic static void printEachCharOccurrence(String s){
System.out.println(Arrays.stream(s.toLowerCase().split("")).
collect(Collectors.groupingBy(Function.identity(),Collectors.counting())));
//Or
System.out.println(s.chars().mapToObj(c->(char)c).
collect(Collectors.groupingBy(Function.identity(),Collectors.counting())));
}

2. Count total occurrence each char in a Array list of string.

public static void validateSameChars(List<String> list){
System.out.println(Arrays.stream(list.stream().collect(Collectors.joining()).split("")).
collect(Collectors.groupingBy(Function.identity(),Collectors.counting())));
}

3. Sort list of string increasing order by length

public statis void sortStringByLength(String s){
System.out.println(Arrays.stream(s.split(" ")).
sorted(Comparator.comparing(String::length)).collect(Collectors.toList()));
}

4. Reverse each string in a sentence.

public static void reverseEachWord(String s){
String s2= Arrays.stream(s.split(" ")).map(e->new StringBuffer(e).reverse()).
collect(Collectors.joining(" "));
}

5. Remove duplicates from a list.

public static void removeDuplicates(List<String> list){
list.stream().collect(Collectors.toSet());
}

6. Remove duplicates from two lists.

public static void removeDuplicates(List<String> l1, List<String> l2){
Stream.concate(l1.stream(),l2.stream()).collect(Collectors.toSet());
}

7. Verify given Strings are Anagram.

public static void anagramTest(String s1, String s2){
String s3=Arrays.stream(s1.toLowerCase().split("")).sorted().
collect(Collectors.joining());
String s4= Arrays.stream(s2.toLowerCase().split("")).sorted().
collect(Collectors.joining());
if(s3.equals(s4)) {
System.out.println("its Anagram.");
} else{
System.out.println("Given String are not anagram.");
}
}

8. Validate same chars in a given array list of strings.

//Validate same chars in a given array list of strings.
//Or group by same chars in a array list of strings

public static void validateSameChars(List<String> list){
Map<String, Double> map = new HashMap<>();
list.stream().forEach(e->{
double d=Arrays.stream(e.split("")).map(i->(int)i.toCharArray()[0]).
reduce(0,(s1,s2)->s1+s2).doubleValue();
map.put(e,d);
});
Map<Double,List<String>> map2 = map.entrySet().stream().
collect(Collectors.groupingBy(Map.Entry::getValue,Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
System.out.println(map2);
}

9. Get last element of an ArrayList. Or Get the nth element of an ArrayList.

public static void getNthElement(List<String>list, int n){
if(n==0){
s=list.size()-1;
}
System.out.println(list.stream().skip(n).findFirst().get());
}

10. Find the first non-repeated character in a string.

public static void getNonRepeatedChar(String s){
Map<String, Long> map= Arrays.stream(s.split("")).
collect(Collectors.groupingBy(Function.identity(),
LinkedHashMap::new, Collectors.counting()));
System.out.println(map.entrySet().stream().filter(e->e.getValue()==1).
findFirst().get());
}

11. Find the first repeated character in a string.

public static void getFirstRepectedChars(String s) {
System.out.println(s);
Map<String, Long> map= Arrays.stream(s.split("")).
collect(Collectors.groupingBy(Function.identity(),
LinkedHashMap::new, Collectors.counting()));
System.out.println(map.entrySet().stream().
filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).
findFirst().get());
}

12 Find Subarray with given sum | Set 1 (Non-negative Numbers)

Given an array arr[] of non-negative integers and an integer sum, find a subarray that adds to a given sum. There may be more than one subarray with sum as the given sum, print first such subarray.

/*Examples: Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Output: Sum found between indexes 2 and 4
Explanation: Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33
Input: arr[] = {1, 4}, sum = 0
Output: No subarray found
Explanation: There is no subarray with 0 sum
*/

public static int [] getSubindexArray(int ary[], int sum){
Map<Integer, Integer> intMap= new HashMap<>();
int currentSum=0;
for(int i=0;i<arr.length;i++) {
currentSum+=arr[i];
if(currentSum==sum) {
return new int[] {0,1};
}
if(intMap.containsKey(currentSum-sum)) {
return new int[] {intMap.get(currentSum-sum)+1,i};
}
intMap.put(currentSum,i);
}
return new int[] {};
}
}

--

--