Java 8 Interview Sample Coding Questions with Explanations

1. Separate Odd And Even Numbers

listOfIntegers.stream()
    .collect(Collectors.partitioningBy(i -> i % 2 == 0));

Explanation: Uses partitioningBy to create a map with two entries: true for even numbers, false for odd.


2. Remove Duplicate Elements From List

listOfStrings.stream()
    .distinct()
    .collect(Collectors.toList());

Explanation: distinct() filters out duplicates; the result is collected into a list.


3. Frequency Of Each Character In String

inputString.chars()
    .mapToObj(c -> (char) c)
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Explanation: Converts each character to a stream and counts occurrences using groupingBy.


4. Frequency Of Each Element In An Array

anyList.stream()
    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

Explanation: Groups elements by value and counts how many times each appears.


5. Sort The List In Reverse Order

anyList.stream()
    .sorted(Comparator.reverseOrder())
    .forEach(System.out::println);

Explanation: Sorts the list in descending order using Comparator.reverseOrder().


6. Join List Of Strings With Prefix, Suffix And Delimiter

listOfStrings.stream()
    .collect(Collectors.joining("Delimiter", "Prefix", "Suffix"));

Explanation: Joins elements with a delimiter and adds a prefix and suffix.


7. Print Multiples Of 5 From The List

listOfIntegers.stream()
    .filter(i -> i % 5 == 0)
    .forEach(System.out::println);

Explanation: Filters numbers divisible by 5 and prints them.


8. Maximum & Minimum In A List

listOfIntegers.stream().max(Comparator.naturalOrder()).get();
listOfIntegers.stream().min(Comparator.naturalOrder()).get();

Explanation: Uses max and min with natural ordering to find largest and smallest elements.


9. Merge Two Unsorted Arrays Into Single Sorted Array

IntStream.concat(Arrays.stream(a), Arrays.stream(b))
    .sorted()
    .toArray();

Explanation: Concatenates two arrays, sorts, and converts to array.


10. Merge Two Unsorted Arrays Without Duplicates

IntStream.concat(Arrays.stream(a), Arrays.stream(b))
    .sorted()
    .distinct()
    .toArray();

Explanation: Same as above but removes duplicates with distinct().


11. Three Max & Min Numbers From The List

// Min 3
listOfIntegers.stream().sorted().limit(3).forEach(System.out::println);

// Max 3
listOfIntegers.stream().sorted(Comparator.reverseOrder()).limit(3).forEach(System.out::println);

Explanation: Sorts the list and limits to the first 3 elements.


12. Anagram Program

s1 = Stream.of(s1.split("")).map(String::toUpperCase).sorted().collect(Collectors.joining());
s2 = Stream.of(s2.split("")).map(String::toUpperCase).sorted().collect(Collectors.joining());

Explanation: Normalizes strings (uppercase, sorted) and checks for equality.


13. Sum Of All Digits Of A Number

Stream.of(String.valueOf(inputNumber).split(""))
    .collect(Collectors.summingInt(Integer::parseInt));

Explanation: Splits the number into digits and sums them.


14. Second Largest Number In Array

listOfIntegers.stream()
    .sorted(Comparator.reverseOrder())
    .skip(1)
    .findFirst()
    .get();

Explanation: Skips the largest number and picks the next one.


15. Sort Strings By Length

listOfStrings.stream()
    .sorted(Comparator.comparing(String::length))
    .forEach(System.out::println);

Explanation: Sorts strings by their length in ascending order.


16. Common Elements Between Two Arrays

list1.stream()
    .filter(list2::contains)
    .forEach(System.out::println);

Explanation: Filters elements that are present in both lists.


17. Sum & Average Of Array

Arrays.stream(inputArray).sum();
Arrays.stream(inputArray).average().getAsDouble();

Explanation: Performs basic arithmetic on array elements.


18. Reverse Each Word In String

Arrays.stream(str.split(" "))
    .map(word -> new StringBuffer(word).reverse())
    .collect(Collectors.joining(" "));

Explanation: Reverses each word and joins back with space.


19. Sum Of First 10 Natural Numbers

IntStream.range(1, 11).sum();

Explanation: Generates numbers 1 to 10 and sums them.


20. Reverse Integer Array

IntStream.rangeClosed(1, array.length)
    .map(i -> array[array.length - i])
    .toArray();

Explanation: Accesses array in reverse order and builds a new one.


21. Strings Starting With Number

listOfStrings.stream()
    .filter(str -> Character.isDigit(str.charAt(0)))
    .forEach(System.out::println);

Explanation: Filters strings where the first character is a digit.


22. Palindrome Program

IntStream.range(0, str.length() / 2)
    .noneMatch(i -> str.charAt(i) != str.charAt(str.length() - i - 1));

Explanation: Compares each character from start and end.


23. Find Duplicate Elements In Array

listOfIntegers.stream()
    .filter(i -> !set.add(i))
    .collect(Collectors.toSet());

Explanation: Uses set to detect duplicates.


24. Last Element Of List

listOfStrings.stream()
    .skip(listOfStrings.size() - 1)
    .findFirst()
    .get();

Explanation: Skips to the last element and retrieves it.


25. Age Calculation

LocalDate birthDay = LocalDate.of(1985, 1, 23);
LocalDate today = LocalDate.now();
System.out.println(ChronoUnit.YEARS.between(birthDay, today));

Explanation: Calculates the number of years between two dates.


26. Fibonacci Series

Stream.iterate(new int[]{0, 1}, f -> new int[]{f[1], f[0] + f[1]})
    .limit(10)
    .map(f -> f[0])
    .forEach(i -> System.out.print(i + " "));

Explanation: Uses Stream.iterate to generate Fibonacci sequence.