I have faced many interviews (too many to be frank) in my career as a software developer. In most of those interviews I have been asked to write a pseudo code for a given problem and implement it in Java. With Java8, this implementation mostly should be in Java8.

When I look back all those questions it can be simplified as below. (difficulty may be vary though).

- Iterate over a given collection (stream)
- Filter the given data (filter)
- Transform into another format (map, reduce)
- Collect data into a collection (collect)
- or End the stream (forEach, min, etc)

Is this familiar to you? This should be. This is what we do in our daily work. But, if you are blindly using it you will see it as a difficult question to answer.

You need to have good understanding about intermediate and terminal operations. And, you need to really practise and use in daily work. It is meaningless to pass an interview without knowing these.

java-8-streams-intermediate-operations

java-8-streams-terminal-operations

Let's dive into some real questions.

### Find the youngest male student by given list

Let's divide this into smaller parts.- youngest -> min

- male -> filter

- list -> iterate

```
Student youngestmale = students.stream()
.filter(student -> student.gender.equals("male"))
.min((s1, s2) -> s1.age - s2.age) //.min(Comparator.comparingInt(s -> s.age))
.get();
```

### Find all numbers divisible by 3

This is an easy question. But you may remember IntStream. And also if you are asked to how to collect the result into a list you need to remember to convert int to Integer. For this IntStream has boxed operation. Let's break down it.- divisible -> filter

```
IntStream.rangeClosed(0, 25)
.filter(number -> number % 3 == 0)
.forEach(System.out::println);
// collect the result
List
``` result = IntStream.rangeClosed(0, 25)
.filter(number -> number % 3 == 0)
.boxed()
.collect(Collectors.toList());

### Find the sum of even number's power of two

This has multiple answers. It gets little tricky when you asked not to use sum operation. But still we have the same format.- even numbers -> filter

- power of two -> map

- sum -> sum (doesn't exist in Stream but in IntStream)

or

- power of two and sum -> reduce

```
// method 1
int sum = IntStream.rangeClosed(0, 5)
.filter(number -> number % 2 == 0)
.map(number -> number * number)
.sum();
System.out.println(sum);
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// method 2
int sum2 = numbers.stream()
.filter(number -> number % 2 == 0)
.mapToInt(number -> number * number) // convert to IntStream
.sum();
System.out.println(sum2);
// method 3
int sum3 = numbers.stream()
.filter(i -> i % 2 == 0)
.reduce(0, (result, number) -> result + number * number);
System.out.println(sum3);
```

### Find the average marks of a student

This looks difficult at first but very simple. It is because IntStream has average operation. We need to covert Stream into IntStream because;- Autoboxing has a performance impact.

- sum, average operations are not in normal stream.

```
OptionalDouble average = subjects.stream()
.mapToInt(Subject::getMarks)
.average();
```

What do you think? Do you have any interesting interview question.

## No comments:

## Post a Comment