Mihai Surdeanu

Example of practical interview question – Java

Hi guys,

Today, I’m going to present you a nice interview question that you can use to interview your candidate. The question is dedicated to Java programming language, but it can be easily updated to your current programming language.

The idea is quite simple: you can start with a small problem and you can continue to ask other questions based on it to cover multiple areas.

Let’s go! The problem is quite simple. We would like to implement a method used for splitting a given list of maps, into multiple groups, based on a category. In my case, the category is a given field from the map. I’m using a map just for this technical interview. In real life, is not recommended to have a map inside a list.

The desired method signature is:

public static Map<String, List<Map<String, String>>> groupItemsByField(
    List<Map<String, String>> items,
    String fieldName

Let’s take an example, just to be clear on what we are trying to achieve:

    Map.ofEntries(entry("id", 1), entry("category", "A")), 
    Map.ofEntries(entry("id", 2), entry("category", "B")), 
    Map.ofEntries(entry("id", 3), entry("category", "A"))
), "category");

The expected output, if I’m printing to console the map, is:

{A=[{id=1, category=A}, {id=3, category=A}], B=[{id=2, category=B}]}

Question 1: Implement the method for doing the split. The first purpose is to test Java 8 knowledge. If you are familiar with Java 8 or above, the implementation is just one line. If not, no problem, you can still test the candidate logic and how he is thinking.

Using Java 8 the solution is simple:

return items.stream().collect(Collectors.groupingBy(item -> item.get(fieldName)));

Starting from this point, you can ask multiple other questions. Some of them are presented below.

Question 2: Generics. Can you please use generics to generify the signature of the method?

Question 3: Streams. Before grouping, I want to filter the data based on two conditions. There is any performance impact if I have two filters defined or just one with both conditions inside? We will iterate for two times over the list?

Question 4: Deep understanding on how Collectors.groupingBy works. What happens if one map doesn’t have the category field and I’m trying to group them after category?

Question 5: Deep understanding on how streams are working. What happens if items parameter is null? A NullPointerException will be triggered?

Question 6: Again, deep understanding on how Collectors.gourpingBy works. What happens with the order of the elements. Is the order preserved inside the lists from the output, compared with input list?

Question 7: Again, deep understanding on how Collectors.gourpingBy works. What happens with the order of the elements in the output map?

As you can see, with a simple question, you can traverse multiple areas and you can test your candidate, without having to ask multiple theoretical questions. I’m not going to provide you the answers for questions 2-7. 🙂

Happy coding!


Pasionat de IT. Pasionat de viață. Pasionat de tot ceea ce înseamnă a face o viață mai bună, plină de înțelegere, ajutor reciproc și iubire de aproape.

Adaugă comentariu


Arhiva personală