0% found this document useful (0 votes)
2 views

downloaded_audio_Cracking the coding interview || Java streams Coding questions

The document features a tutorial by Mansi from Java Techies, focusing on Java 8 Stream API with 14 to 15 coding questions related to employee data. It covers various operations such as counting male and female employees, calculating average ages and salaries, and filtering employees based on specific criteria. Each question is accompanied by code snippets demonstrating the use of Stream methods like grouping, filtering, and collecting results.

Uploaded by

Andrei Tapia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

downloaded_audio_Cracking the coding interview || Java streams Coding questions

The document features a tutorial by Mansi from Java Techies, focusing on Java 8 Stream API with 14 to 15 coding questions related to employee data. It covers various operations such as counting male and female employees, calculating average ages and salaries, and filtering employees based on specific criteria. Each question is accompanied by code snippets demonstrating the use of Stream methods like grouping, filtering, and collecting results.

Uploaded by

Andrei Tapia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Audio file

downloaded_audio_Cracking the coding interview || Java streams Coding questions.mp3


Transcript
00:00:00
Hello everyone so this is Mansi from Java techies today. I've got around 14 to 15
questions based on stream for all of you based on the current demand for Java 8 and
stream API in interviews like most of the people ask some questions related to
streams and ask you to write code snippets.
00:00:20
So this is not a theoretical session. We will be just going on with a few questions
that will be with with the quote. So let's just start.
00:00:29
So here I have created a list of employee which is employee list. If you see the
employee class so my employee has an ID which is integer it is a.
00:00:40
It has a name.
00:00:41
It has integer age, it has gender, it has department to which it is belonging and
year of joining along with the salary.
00:00:49
That is in double so the.
00:01:04
These are the things that I have as a part of employee, just to be pretty, just to
be on the safe side. So I have I have added a lot of details you can see here to to
my list employee list.
00:01:20
So these are all hard coded details over here, so we'll go for the questions 1 by
1, starting from easier ones to the difficult ones and I'll show you the solution
as well as I'll run the.
00:01:33
So the first one is how many male and female employees are there in the
organization. So this is this is the data this employee list is basically having
all the data for an organization having a list of employees where this is the
gender you can see here, this, this, this particular thing is the gender. So they
are male and females.
00:01:53
So the code that I have written for it, I'll just.
00:01:56
Go through this method.
00:01:59
Yeah. So I need to actually find out how many male employees are there and how many
female employees are there. Just the count of both of those. So I have used here on
the employee list, I have used the stream and then I have done a collect which is a
terminal operator. I'm using the collect method and then I'm using the grouping.
00:02:18
By so I'm using grouping by and I'm doing grouping by by get gender because I need
to.
00:02:23
Through the genders of female and male employees, and I don't need any details. Who
are the female and who are the male? I just need the counting. So I'm using the
counting method over here which is there in collectors.
00:02:37
So using this I can simply get the count of female and female employees. Let's just
run this.
00:02:46
So you can see there are 11 main male employees and six female employees.
00:02:53
Now let's go to the second question.
00:02:57
Yeah. So you can.
00:02:58
See print the name of all the departments in the organization.
00:03:05
So here we just simply want to print the distinct departments that are there in the
organization. So I have taken again the list and I've used stream over it and using
the map method now. So if I use the map method from the employee through method
reference, I can use the get department.
00:03:24
And I can get all these departments and using the distinct methods so it will
provide me distinct that is unique. The unique names of department and then using
for each and simply printing them out.
00:03:38
Let's just.
00:03:41
Run this.
00:03:46
Yeah, so here you can.
00:03:48
See let me just scroll up.
00:03:53
Yeah, so here you can.
00:03:54
See.
00:03:55
These are the list of all the distinct departments. There is no repetition amongst
them and you can see here I got all the list of distinct departments.
00:04:05
For the organization.
00:04:07
Now let's let's move on to the next one. So the third one is what is the average
age of male and female employees?
00:04:15
So in order to find the average age of male and female employees.
00:04:21
This is the below code snippet so again I'm taking the list and using stream. Now
I'm doing a collect and I'm again using grouping by by gender and if you remember
in the last one we had to get the counting of male and female employees. Now we
want the average age. So here we were using collectors.
00:04:41
Not counting. Now you are. We are using averaging int so then since this is an
integer we are using averaging averaging int. If this is a double you can use
averaging double and other methods like that and I'm simply providing the age of
every employee for that so this will give me the average age.
00:05:03
If I run it, let me just show you the output so you can see average age of male
employees. Is this and female employees is this?
00:05:16
So here we are just simply grouped by the gender and then we are using the
averaging INT method to find the average age.
00:05:30
Now the next one is get the details of highest paid employees in the organization.
00:05:35
Meaning to say we need to get the details of the employees in the organization who
have the highest pay scale. That is the most the maximum number of salary. So again
I use the collect and then there is a method Max buy so inside the Max buy.
00:05:55
You can see that you have to provide the comparator.
00:05:59
So I am using the Max by and in the comparator since the salary is in double so I
am using comparing double if it is integer or something like that. You can use the
equivalent for it like comparing integer or things like that so you can see. So I
am using inside the collect. I am using Max by and.
00:06:19
Inside that I'm providing the comparator which compares the salary.
00:06:25
So let's just run this one.
00:06:33
So here you can see for the 4th 1:00.
00:06:36
The maximum salary of this employee is actually the maximum salary. Let me just
show you. OK here we can see.
00:06:48
This is the salary column and I think this is this is the one we got as a result.
So his salary you can compare among with the rest of the one is maximum that is
35,700 because of which we got his name as the output.
00:07:09
Now moving on to the next question.
00:07:13
Which is the 5th question. So get the names of all the employees who have joined
after 2015. So there are some questions that people usually ask to get the name of
all the employees who have joined after this this particular year or some some
other query or similar kind of query. So here we are going to use filter because we
need to perform.
00:07:35
Some kind of filtration for the employees who have joining date that is after 2015.
So you can see inside the.
00:07:44
I have done a E dot get year of joining which was a part of my I I was saving year
of joining as well who's whoever has the year of joining greater than 2015. I'm
simply after the filter I'm mapping it using the map method with the name.
00:08:02
So I'm just mapping the name of the employee and then using the foreach I'm just
printing it out.
00:08:07
Let's just run it.
00:08:15
Yes. So you can see these are the name of the employees who have joined after 2015.
Let's move on to the next question that is the question number six, count the
number of employees.
00:08:30
Each department. So again we need a counting of number of employees in of the each
each of the depot.
00:08:37
So again, you can see we are using similar kind of logic that we used for counting
of male and female employees. So you can see inside the collect we are again using
group by. Now this time we are grouping by the department because we need the count
of employees per department. So I'm doing a gate department and again I'm using.
00:08:57
Letters, not counting to count that thing. This is returning a map.
00:09:03
So I'm simply printing the keys and.
00:09:06
Values over here.
00:09:12
Let's just run this.
00:09:18
So you can see this is the number. This is the name of the department and the count
that we have for the number of employees that are there in each department. Now
moving on to.
00:09:32
The next question.
00:09:35
What is the average salary of each department? So now we need to find the average
salary of each department so you can see over here we are again using grouping by
because we need to find the average salary per of each department. So we will have
to group by the department and then there is an averaging.
00:09:54
Double method that we can use. So now earlier on we were using collectors dot
counting and things like that. So there is collectors dot averaging double as well
which finds the average of double data type. So since get salary is double so we
are using averaging double.
00:10:11
Over here. So it will give me the list of. Sorry, the average salary of each of the
department.
00:10:18
Let's just run this.
00:10:28
So you can see I am getting the name of the department along with average salary
and here I'm printing the key and into key and value pairs. So this is key is the
name of the department and value is the average salary of each of the department.
00:10:45
Now moving on to the next question.
00:10:50
So get the details of youngest male employee in the department. That is the person
who has least age minimum age. You can say so we are using the filter over here and
inside the filter basically we have to find out.
00:11:08
We have to actually find out the employee of Product Development Department and he
should be a male. These are the two conditions that we need to put forward. So
inside the filter you can see I'm checking if the get gender is male and the get
department is product development. Then only I'm finding the minimum.
00:11:26
And inside the minimum method you have to again provide the comparator. So I've
used comparator dot comparing int since my age is integer. That is why I'm using
comparing int over here and then I'm using I'm I'm providing it.
00:11:40
The age.
00:11:42
And simply printing this out.
00:11:53
So here you can see. So he is the youngest employee, Nitin Joshi.
00:12:01
Along with this idea, I've printed his details. You can also, if you want, print
other details as well. Now moving on to the next question.
00:12:19
Who was the most who has the most working experience in the organization?
00:12:27
Stop.
00:12:29
Code I have written for this, so we need to find a person who has the maximum work
experience. So here you can see I have again used the sorted method over here to
sort the entire list based on the year of joining so you can see I have used the
sorted method inside the sorted I have.
00:12:49
I have.
00:12:50
The comparator again using the comparing end for the year of.
00:12:54
Joining so the person so so this entire list will be now sorted based on the year
of joining of a particular person. So the one who has joined first will have the
maximum amount of work experience. So I have just done the find 1st to get that the
details of that particular.
00:13:15
Employee and then I'm simply so it returns an optional of employee. I'm doing an
optional dot get and then simply getting all the details of that particular
employee.
00:13:26
Let's just run this piece.
00:13:28
Of code.
00:13:32
So you can see here I have got the details of Manu Sharma who has the highest work
experience.
00:13:41
In the organization. Now let's move on to the next question. Next is how many male
and female employees are there in sales and marketing?
00:13:54
Again, there is a filter that I want. I I want the details for only sales and
marketing department so you can see inside the filter I have put a check if my
department is equal to sales and marketing then only I'm grouping.
00:14:11
By gender and finding out how many people are there, how many female employees are?
00:14:17
In.
00:14:18
This particular department, sorry, male and female both. So since here we are
putting a check on sales and marketing department and then we are grouping by
gender and simply using the counting to find out the count.
00:14:37
So you can see inside the sales and marketing department, there is one female
candidate and two male candidates.
00:14:46
Now let's move on to the next question.
00:14:50
What is the average salary of male and female employees?
00:15:00
So in order to find the average again we are using in the list. We are using the
stream and then we are using the collect terminal operation into which we are
grouping it by gender. Because we want the average of male and female.
00:15:15
Employees. So we are grouping it by gender and then we are using averaging double
on salary to get the.
00:15:24
To get the salary, every salary of male and female employees.
00:15:29
Let's just run this.
00:15:30
One South here you can see we got the average salary for the male and female
employees of the organization. Now let's move on to the next.
00:15:38
1.
00:15:45
List all the names of all the employees in each department. So now we have to
filter based on each department. We have to actually find the list of employees for
every department. So here simply we are using the collect operation and then we are
doing a grouping by department.
00:16:04
Using this we will get all the details.
00:16:07
And then simply printing it out.
00:16:11
Let me just run.
00:16:12
It so here you can see I got the output.
00:16:16
So here. OK, so here you can see in the product development department, these are
the list of employees in the security and transport. These are the list of
employees, sales and marketing, infrastructure, HR and account and finance. So you
can see I'm getting the list of employees for each of the department.
00:16:40
Let's move on to the next question.
00:16:47
So what is the average salary and total salary of?
00:16:50
The whole organization.
00:16:54
So you can see again we are using the collect operation and then we are using the
summarizing double and providing the salary into it. If in this method it actually
returns double.
00:17:06
Summary statistics. So what is this? This actually maintains all the statistics of
of a particular. You can say object so it it it it. Using this we can actually get
the average and sum out of it out of this particular.
00:17:23
Reference and yeah, let's just let's just run this one.
00:17:31
You can see here we got the average and total salary. So this is because of this
double summary statistics which gives us the average and total sum.
00:17:45
Now let's move on to the next question.
00:17:48
So OK, separate the employees who are younger or equal to 25 years from those who
are older than. So there is a bar of 25 years. We have to get the list of employees
who are from who are 25 years or below 25 years, and another list which who are
above 25 years.
00:18:08
So for this we will be using the partitioning bag, so this does it. It actually
partitions based on condition.
00:18:16
So you can see inside the collect I am using this partitioning by and I have
provided each barrier as greater than 25. So this will create a couple of
partitions. The first one will be from 25 or below 25 and another one will be
greater than.
00:18:32
20.
00:18:33
Five. So I have simply printed this entire thing out. I will.
00:18:36
Just the.
00:18:37
Run this piece of code.
00:18:42
So you can see.
00:18:43
Here.
00:18:45
Employees younger than or equal to 25. This is the list and employees older than 25
years. So this is the list of employees who are older than 25 years. Now let's move
on to the last question.
00:19:01
Yeah, this one.
00:19:04
Who is the oldest employee in the organization?
00:19:07
What is his age and which department he belongs to? So basically we have to find
the oldest employee of the organization along with certain details so you can see
we have used the Max method over here and inside the comparator we have simply
provided comparing in and as a parameter to it. We have provided the age
constraint.
00:19:27
So what it will do is it will find the employee with maximum age and then simply we
have printed out all the details of that particular employee.
00:19:37
Let me just run it.
00:19:44
Yes. So you can see here these are the details. So his name is Iqbal Hussain and
his age is 43 and his department is security and transport. So you can see here we
got all the details and these were the set of 15 questions that I had for all of
you. Hope this was.
00:20:01
Informative and you will be able to learn more about Java 8. So these are a few
commonly asked questions and something similar to this. If you get that basic
understanding of these these many items or a few more, you will be easily able to
crack any of the.
00:20:16
Court questions that have been asked by the interviewer for you to write some code
in Java 8. So thanks. Thank you everyone. Thanks for watching to like, share
subscribe our channel. Thank you so much. Have a good day.
00:20:28
Bye bye.

You might also like