Capgemini Java Interview 2024 | Java | Spring Boot | Microservices | Database
Summary
TLDRThe interviewee discusses their Java knowledge, including streams, collections, and immutability. They explain HashMap vs ConcurrentHashMap, thread safety, and the volatile and synchronized keywords. They also cover Java memory model, exception handling, and their experience with Spring Boot and microservices. They detail the shift from monolithic to microservices architecture, challenges like data consistency, and query optimization strategies.
Takeaways
- 💻 The interviewee is proficient in Java and is being tested on Java Stream to find the second highest number in a list.
- 📝 The interviewee explains the process of defining a list, using streams, distinct elements, sorting in descending order, and skipping elements to solve the problem.
- 🔑 The use of 'Optional' in Java is highlighted for handling cases where a value may or may not be present.
- 🗃️ The interviewee has experience with Java collections, differentiating between 'HashMap' and 'ConcurrentHashMap', emphasizing thread safety.
- 🧵 The interviewee discusses Java's threading capabilities, explaining 'synchronized' for critical sections and 'volatile' for variable visibility across threads.
- 🌐 The Java memory model is briefly described, including Young Generation, Old Generation, and Permanent Generation.
- 📝 The interviewee is asked to create an immutable class similar to 'String', emphasizing final class, final fields, and providing only getter methods.
- 🛠️ Exception handling in Java is covered, distinguishing between checked and unchecked exceptions, and the use of try-catch blocks.
- 🌟 The interviewee has experience with Spring Boot and microservices, highlighting advantages like starter dependencies, auto-configuration, and embedded servers.
- 🛡️ In Spring Boot projects, exceptions are handled at both global and method levels using@ControllerAdvice and @ExceptionHandler annotations.
- 🌐 The transition from monolithic to microservices architecture in the interviewee's project is discussed, focusing on scalability, independent teams, and service communication.
Q & A
What is the main objective of the interview?
-The main objective of the interview is to assess the candidate's knowledge and experience in Java, Spring Boot, microservices, and related technologies.
How does the candidate demonstrate their knowledge of Java Streams?
-The candidate demonstrates their knowledge of Java Streams by explaining how to find the second highest number in a list of integers using distinct, sorted, and skip methods.
What is the difference between HashMap and ConcurrentHashMap as explained by the candidate?
-HashMap is not thread-safe, while ConcurrentHashMap is thread-safe. ConcurrentHashMap allows multiple threads to access and modify it without causing inconsistencies by dividing it into segments.
How does the candidate describe the Java memory model?
-The candidate describes the Java memory model as consisting of a Heap memory divided into Young Generation, Old Generation, and Permanent Generation, with different garbage collection frequencies.
What is the candidate's approach to creating an immutable class in Java?
-The candidate suggests making the class final, defining only getter methods for variables, and providing values through a constructor to achieve immutability.
How does the candidate handle exceptions in Java?
-The candidate handles exceptions by using try-catch blocks for checked exceptions and mentions that unchecked exceptions do not require try-catch blocks but can be logged or handled when they occur.
What are the advantages of Spring Boot mentioned by the candidate?
-The candidate mentions that Spring Boot simplifies dependency management with starter dependencies, provides auto-configuration, and includes an embedded Tomcat server for easy deployment.
How does the candidate handle exceptions in a Spring Boot project?
-The candidate handles exceptions at both global and method levels using @ControllerAdvice for global exceptions and @ExceptionHandler for method-level exceptions.
What is the difference between authentication and authorization as explained by the candidate?
-Authentication is about verifying who a user is, while authorization is about determining what resources a user is allowed to access.
How does the candidate describe the transition from a monolithic to a microservices architecture?
-The candidate describes the transition as a move from a single large application to multiple smaller services, each managed by different teams, allowing for independent scaling and updates.
What challenges does the candidate identify with managing multiple databases in a microservices architecture?
-The candidate identifies data consistency and transaction management as major challenges when dealing with multiple databases in a microservices architecture.
Outlines
💻 Java Stream and Interview Coding Exercise
The paragraph discusses an interview scenario where the candidate is asked to demonstrate Java stream knowledge by finding the second-highest number in a list. The candidate explains the process of defining a list of integers, using Java streams to get distinct elements, sorting them in descending order, and then finding the second element after skipping the first highest. The candidate also mentions the use of Optional to handle cases where the result might not be present. The summary also touches on the importance of the skip method in finding the second element after sorting.
📚 Collections, Thread Safety, and Java Memory Model
This section covers the candidate's experience with Java collections, specifically lists and maps, including their implementations like ArrayList, HashMap, and LinkedList. The candidate explains the difference between HashMap and ConcurrentHashMap, emphasizing thread safety. The conversation then shifts to Java's memory model, including Young Generation, Old Generation, and Permanent Generation, and how garbage collection works across these areas. The candidate also discusses the volatile and synchronized keywords in the context of thread safety and memory management.
🔐 Immutable Classes and Exception Handling
The candidate discusses how to create immutable classes in Java, similar to the String class, by making the class final and only providing getter methods without setters. This ensures that once an object is constructed, its state cannot be changed. The paragraph also covers exception handling in Java, distinguishing between checked and unchecked exceptions, and explains the use of try-catch blocks to handle exceptions. The candidate provides an example of a null pointer exception and how to handle it with a try-catch block.
🌟 Advantages of Spring Boot and Exception Management
The candidate talks about the advantages of Spring Boot over earlier versions of Spring, such as reduced boilerplate code with starter dependencies, auto-configuration, and embedded servers like Tomcat. They also discuss exception handling in Spring Boot, mentioning both global and method-level exceptions, and how to map exceptions to HTTP status codes. The paragraph includes examples of various HTTP status codes used in projects, like 401 for unauthenticated users and 500 for internal server errors.
🛠 Transition from Monolithic to Microservices Architecture
The candidate describes transitioning a project from a monolithic architecture to a microservices architecture, detailing the components involved such as API Gateway, Discovery Server, and individual microservices. They explain how each microservice has its own database and how they communicate synchronously or asynchronously. The paragraph also touches on the challenges of maintaining data consistency and transaction management in a microservices environment and the use of tools like Actuator for monitoring.
🚀 Query Optimization and Microservices Database Challenges
In this section, the candidate addresses challenges related to database management in a microservices architecture, particularly data consistency and transaction management. They discuss strategies for optimizing slow database queries, such as using indexed columns, reviewing query execution plans, and denormalizing data to improve performance. The paragraph concludes with the candidate not having any questions for the interviewer.
Mindmap
Keywords
💡Java Stream
💡Spring Boot
💡Microservices
💡API Gateway
💡Discovery Server
💡Concurrent HashMap
💡Volatile Keyword
💡Synchronized Keyword
💡Immutable Class
💡Exception Handling
💡Java Memory Model
Highlights
Introduction to the interview focusing on Java, Spring Boot, and microservices.
Candidate's familiarity with Java streams demonstrated through a coding exercise.
Explanation of how to find the second highest number in a list using Java streams.
Discussion on the importance of using distinct and sorted methods in Java streams.
Clarification on the use of the 'skip' method in Java streams to find the second highest number.
Understanding of collections such as List and Map in Java projects.
Difference between HashMap and ConcurrentHashMap in terms of thread safety.
Experience with Java threads and knowledge of 'volatile' and 'synchronized' keywords.
Description of the Java memory model including Young Generation, Old Generation, and Permanent Generation.
Immutability concept and how to create an immutable class similar to the String class in Java.
Handling exceptions in Java programming with try-catch blocks.
Advantages of Spring Boot over earlier versions of Spring, including starter dependencies and auto-configuration.
Use of different HTTP status codes in Spring Boot projects to handle various scenarios.
Difference between authentication and authorization in software access control.
Explanation of Spring Boot profiles for different environments like Dev, QA, and Production.
Transition from monolithic to microservices architecture in the project.
Challenges faced with database management in a microservices architecture.
Strategies to optimize slow database queries, including indexing and query execution plan analysis.
Interview conclusion with the candidate having no further questions.
Transcripts
hey hi how are you hey hello I'm fine
thank you how are
you I'm good thank you so let's get
started with the interview I would like
to ask you some questions about Java
then we'll go through springbot and
microservices so let's get started with
the coding okay okay so do you know Java
stream yes yes I know jav
stream okay so you can find a list of
numbers and you have to find the second
highest number from that list so you can
consider your own list and start writing
the course okay so I have to uh Define a
list of integer and from that list of
integer I have to find the second as am
I right yes correct okay so let me start
by creating a class I'll I'll try to to
find second
highest I'm also going to add the main
method to it so that this is going to be
starting point of my application and
then I'm going to finish it uh so the
first step is to define a list what I'll
do I'll take uh list of uh
integer and I'm going to call this as
numbers equal to new uh not new arrays
dot as list and I'm going to Define 1 5
7 8 9 okay so these are my numbers I'm
going to import list and ARR list let's
try to import list and arrays both of
them right so I'll write the problem
statement here first
um find the
second highest number using
Java steam Ms
okay now let's try to uh solve this
numbers do the first step is to get the
stream out of your list so I'm going to
get the stream now once I get the stream
uh I'm going to do distinct to get the
distinct element suppose if some of the
numbers are repeated twice so I want to
remove uh those numbers and consider
them only once once I take the distinct
then I'm going to sort this list in
descending order so the big bigger
number will come first and then from
there uh it will keep on
descending for that I'm going to use
sorted the sorted accepts a comparator
so it Compares two numbers so those two
numbers are going to be a and b in our
case and uh as we have to sort in
descending order we are going to return
B minus a so this will help us sort the
number in uh descending order so once I
sought the number as we want to find the
second highest we are going to skip
the first number I'm going to skip the
first number and then we have to take
the number that we find after that for
that we will do find first okay so this
will give me the optional let me take
this in
optional optional is a construct where
value can be either present or uh won't
be present this is optional of integer
so I'll give optional equal to this
right
after this I'm going to get the result
optional. get okay so this will give me
the result and this will be a integer
result
integer second highest okay and finally
I'm going to uh print print the number
so let's try to print second highest
okay so I'll I'll try to uh run this
okay so second highest in our case is
eight because the first highest is nine
and the second highest is eight so here
is the program using jav
streams okay and what is that skip
method used for uh skip so once I sort
the elements so the elements will be 9 8
7 5 1 and one so it will be sorted in
descending order then I have to skip the
first element which is nine so that is
where we are going to get the second uh
number we are skipping the first and
then we are targeting the second
element and that optional is used for
optional is used uh for example uh
sometimes we don't have results in that
case we use optional optional is where
the result can either be present or
cannot be present so it gives you some
of the methods where you can check if
the value is present or not present and
then you can fetch the value from
option okay good so do you know the
collections
yeah I know
collections so what are the collections
which you have used in your project yeah
right so uh interfaces I've used uh list
uh map and when it comes to the
implementation I have used AR list uh
hashmap link list concurrent
hashmap okay can you tell me the
difference between hashmap and
concurrent hashmap okay so hashmap is a
key value pair and even concurrent
hashmap is a key value pairer but there
is a basic difference in terms of
threade safety so hashmap is not thread
safe and concurrent hashmap is thread
safe for example if multiple threads are
trying to access hashmap that can result
in inconsistent state of the hashmap to
solve this problem uh Java came up with
concurrent hashmap where multiple
threats can access concurrent hashmap
without making the result inconsistent
so in case of concurrent hashmap there's
a concept of segments so you divide
divide your congr hashmap into multiple
segments and each thread can access one
segment at a time so if there are four
threads and four segments and four
threads can concurrently access four
segments and they can make changes in
those segments so that is how concurrent
hashmap the working concurrent hashmap
is
done okay fine do you have any
experience working with the threads in
Java yeah yeah I have experience working
threads okay have you heard about
volatile keyword or synchronized keyword
in Java yeah yeah correct so I'll start
with synchronized so when you try to try
to run your programs related to threads
so there is uh something called as
critical section the critical section uh
is a piece of code that only one thread
can execute at a time so if two threads
are trying to execute that section at
the same time so that will result in
problems so that is where synchronized
comes into picture when you say some
method is synchronized so that method
can execute that that method can be
executed by one thread at a time so that
is the significance of synchronized
keyword whenever you mark something as
synchronized then only one thread can
enter into that block or that method and
execute it right so this is about
synchronized method and uh volatile is
where um when you mark your variable as
volatile then it will uh make sure that
no thread will cach that value and that
will always be read from the memory
whenever thread one wants to read it it
will go to the memory and read it and
when thread two wants to read it it will
go to the uh memory and read it so no
one is going to catch it at their level
so that is where volatile comes into
picture okay so you said that volatile
will be used to read the data from
memory directly right correct correct
and no
catching okay and do you know the Java
memory model uh yeah so Java memory
model uh is basically uh the Java uh
when you running your program then uh
some of the memories allocated in your
RAM for Heap that is your HEAP memory
now your HEAP memory is divided into uh
multiple uh sections uh known as uh
Young Young Generation old generation
and permanent generation So Young
Generation is where newly created
objects live okay and old generation is
where when your object reaches some age
then they are shifted to Old generation
and there are some objects which
permanently reside in your heat uh so
that that resides in your permanent
generation so when it comes to garbage
collection your Young
Generation area is garbage collected
frequently so and your old generation
less frequently and permanent generation
is uh never garbage collected and in
some cases very less garbage collected
so this this is the memory model of
java okay good okay uh do you know
stream class in Java string string
string string string string yeah I know
string class so that string class is
immutable right correct yeah okay now
can you tell me if I need to create a
class which is immutable similar to
string so can you do it or how to do it
okay do you know how to make it
immutable correct correct so immutable
is where when once you construct the
object the state of that object cannot
be changed for example if I create a
string ABC and if I want to append uh
pqr to the string then the existing
string does not change uh when you
append pqr a new string will be created
so this is the concept of um
immutability now how to achieve uh this
in Java so to uh achieve this the first
thing that we do is make a class and
Mark that class as final so that no no
one should be able to extend that class
and uh the second thing is we Define
variables uh so for those variables uh
we only Define uh stter methods we don't
Define GAA methods so this is how we are
only able to uh Set uh the values of the
fields and we should never uh we should
never be able to uh uh give me a moment
so we Define Setra
methods uh can you give me a hint over
here okay so you should not be able to
set the values right you should be able
to retry the values or read the values
so you need to provide the gets only
right gets only so what we do um we
Define a
Constructor okay and in Constructor we
provide the values so we don't provide
the stter methods we only provide the
geta methods because we should only be
able to get the value we should never be
able to set the value so that is where
we skip the seter
methods okay
fine okay uh so how do you handle
exceptions in your programming uh all
right so there are two types of
exceptions uh one is the checked
exception and one is the unchecked
exception and for unchecked exceptions
we also tell that as runtime exception
okay so checked exceptions are the one
which you are forced to handle for
example IO exception okay so this is a
uh checked exception Whenever there is a
checked exception uh then you have to
surround that with a TR catch block for
example like this okay IO exception and
for unchecked exception uh you don't
necessarily have to uh surround it with
a tri triage block and generally
unchecked exceptions are your logical
problems for example um consider this I
create a string s Str equal to null for
example and then I do Str Str do cared Z
so this is going to uh return a null
pointer exception so this is my runtime
exception and as you can see I'm not
handling it let's run this
program okay so it gives you null
pointer exception but I don't have to
surround this with a tri catch block so
this is my unchecked exception or
runtime
exception okay great and what is that
catch block is used for if you surround
it with try catch block
okay so catch catch block is used to uh
catch the exception and do some U
processing for example let me do this so
try uh catch and I'm going to catch
exception e okay so whenever there is a
problem in try for example uh let's do
this in I equal to uh five okay and then
what I do I do I uh equal to I divided
by zero okay and we know that when we
divide something by zero then we are
going to get the exception oh let's try
this Whenever there is an exception we
perform something so for example we do
logging or we throw another runtime
exception for our case to make it
simpler I'm going to Sis out um
exception uh during division okay all
right so now I run this
program and you see exception during
division okay so we should never try to
do this we should always check if the
denominator is zero and if it is zero
then we should not go ahead with the
division
operation okay great do you have
experience on Spring Boot and
microservices as well right uh yeah I
have worked on Spring Boot and micros
Services okay so what are the advantages
of spring boot over earlier version of
spring can you tell me few advantages of
spring boot correct so uh there are
various advantages of spring boot for
example in Spring um we to write a lot
of boiler plate code we have to manage
dependency so in case of spring boot uh
they came up with the uh starter
dependency for example spring boot
starter web okay so in case of earlier
version for example spring whenever you
want to write rest apis then you have to
manage the dependency yourself you have
to go to your pom.xml and Define all the
compatible dependencies yourself for
example for web you need uh Jackson
dependency so you need to M the version
of Jackson and you need to maintain the
compatible version of all the
dependencies in case of spring boot uh
there is a concept of starter web
dependency so if you just add the
descriptor springboard starter web it
brings all the chars required to build
rest apis that is the First Advantage
the second Advantage is the auto
configuration where um if based on the
jar that you put in your class bar So
based on the dependencies spring boot
Act actually creates beans for you for
example if you add uh the dependency for
uh uh the database in your uh spring
booat and springboat will uh create a
data source be automatically for you and
the third Advantage is it has embedded
Tomcat for example uh if you create a
web application then in the earlier um
versions of spring you have to create a
jar or War file and deploy it into your
uh tomat so spring boot has given a
mechanism where there is embedded tocat
and you don't have to deploy a jar or
War if you start your jar uh that
automatically gets deployed to your
embedded tomcat and starts running on a
port these are the few advantages of
spring boot over uh
spring okay and how do you handle
exceptions in your spring boot
project okay so there are uh exceptions
at two level uh the first exception is
at the global level and uh exception uh
is at the method level so the global
level exception you can handle it using
advice controller advice annotation and
uh the method at the uh the exception at
the method level you can handle you
using adate exception Handler okay so
whenever uh any exception happens then
in that case uh the response that we
send back to the client is in the form
of status code so generally Whenever
there is an exception you map the
associated exception to the status code
and then you send back that status code
as a response to the
request okay so what are the different
HTTP status codes which you have used in
your project okay so we have used
several status code for example we have
used 401 where the user is
unauthenticated uh then we have used 403
where the user is trying to access a
resource but uh he's forbidden to access
that resource then after that 404 404
where user is trying to access something
but that resource is not present onto
your server um then I have also used 429
where you are making a lot of request to
the server but uh uh those many requests
are not allowed at a time for a server
this is a concept of rate limiting 4 4
to9 and 500 where uh there is any
internal server problem internal server
error has happened during execution of
the program these are few that have used
okay you mentioned 401 as unauthorized
can you tell me what is the difference
between authentication and authorization
all right so authentication is where uh
you tell the system who you are for
example when I'm trying to log to
Facebook U then I give my user ID and
password so this way I tell Facebook
that I'm toik so in authentication you
tell who you are and in authorization
actually authorization is where you tell
what section of program what section of
software where you can access okay so
authentication is who you are and
authorization is where you are permitted
to access which part of the
software okay and do you know the
profiles in your
springboard yeah so profile is where you
can have uh conditional configuration or
conditional beans based on the different
uh environments for example there are
three environments uh Dev U and
production and you want different
configuration for all these three
environments then you can have uh three
different profiles one for Dev one for Q
and one for production and while
starting up your application you can set
up the active profile spring. profiles.
active is the uh uh key which you can
use and you can uh pass uh the name of
the profile which you want to Mark as
active so that active profile
configuration will be picked during
startup and that will be used uh
throughout the uh
program okay good can you explain me uh
the architecture of your project how it
is designed is it based on microservices
or is it monolithic architecture all
right so earlier my project was
monolithic then we shifted to
microservices so now it is microservices
architecture so my project is divided
into multiple uh microservices okay so
there's micros service one micros
service 2 and micros service 3 so
generally whenever client wants to um
make a request to my project so client
will generally give a request to the API
Gateway and API Gateway in turn will
communicate to all the microservices
okay so the first component that resides
is my API Gateway that is implemented in
uh zul zul is the API Gateway then we
have microservices which are small
springbot applications okay and whenever
they start up they register themselves
with something called as Discovery
server which is urea okay whenever
micros service starts it registers
itself with the discovery server so uh
if I have three microservices then all
three microservices will register
themselves with the discovery server and
when one microservice wants to um
communicate with the other microservice
then the address of that microservices
will be given by the discovery server
rather than hard coding in that micros
service so that is the second uh uh
aspect third is each micros service has
its own database so only that
microservice is allowed to communicate
with the database and get the data from
it so no other micros service can
directly communicate uh to the database
they will always go to the rest API um
to get the data and for monitoring and
for uh all this we use actuator so every
micros service has actuator in it and we
go ahead and hit the actuated URL uh to
get the performance and monitoring
information okay so you had a monolithic
architecture and now you are converting
it into microservice based architecture
right so what is it which by means
through which you decided that I will go
for microservices architecture and not
for monolithic architecture okay so what
were the advantages of microservices
architecture over your monolith
architecture so monolithic is where
there is one big application okay so one
big application and one big team and if
I want to change something then that
change after that change I need to test
the entire application okay and also uh
I need to scale the individual
components for example the first thing
is consider Amazon application so we
have buyer functionality we have seller
functionality we have payment
functionality and we have notification
functionality for example I'm getting a
lot of request for my payment
functionality so consider this is one
big montic application then to scale
this application I have to do two
deployments but uh as you can see I just
want to scale up my payment related
aspect I don't want to scale the entire
application still I have to do two
deployments so that is the reason we
thought of dividing our application into
microservices where we can scale the
individual components rather than the
entire application now the application
look like looks like uh buyer uh
microservice seller microservice uh
payment microservice and notification
microservice now if I want to scale up
the payment microservice what we do we
just make two deployments of our payment
microservice uh rather than the entire
application and each uh component each
microservice is managed by uh different
teams now we have autonomous teams who
can manage the microservices uh
themselves uh so they just send the
contract to the other team and then they
consume uh uh the apis using that
contract okay okay and how these
microservices communicate with each
other okay uh so there are two ways of
communication one is synchronous way
where micros service one calls micros
service 2 then micros service one has to
wait until micr Service Tool responds
back and both the parties have to be
present so this is a synchronous
communication and this generally happens
over sttp okay where both the parties
are present so we use either rest client
or fine client rest template or fine
client to do this kind of communication
then the second type of communication is
asynchronous where uh you can do the
communication but whenever the second
party becomes available it will respond
back so in the second case we have
something called as message broker or
event streen for example Kafka in
between so micros service one publishes
something to Kafka and then micros
service 2 consumes that thing from Kafka
and does the operation so synchronous
and asynchronous are the two ways of
communication okay and you said that
your microservices are for database
means you have a separate database for
each micros service yeah
correct okay so do you see any
challenges associated with to our
database management oh correct so uh
there are several challenges when it
comes to microservices for example one
of the biggest challenge is uh keeping
the data consistent is the Big Challenge
as uh it is into multiple data store so
transaction management becomes very
difficult in case of microservices and
data consistency is also one challenge
but there are various mechanisms for
example um uh sagas you can use
orchestration or choreography Saga uh
for transaction and that will help you
maintain the consistency of your data in
case of microservices
architect okay and uh let us consider
there is a query which is taking too
much time to read the data from the
database so in that scenario how do you
handle this situation okay so uh the
first thing that we do uh the query that
is taking a lot of time the first thing
we we do is to check the we Clause if we
Clause is is using any of the columns
where um if there are indexes on those
column if we are using something in a we
clause and if that column does not have
index then it takes a lot of time um if
that is the case then we try to check
can we use columns which are already
indexed and if uh we don't find any such
column then we try to add indexes uh to
the table so this uh increases improves
the time of the query the second thing
that we check is query execution plan so
based on the query execution plan we get
a lot of details and based on that we
can optimize our query further to make a
better execution uh plan so that is the
uh second thing and the third thing is
uh we try to denormalize uh some uh data
so that denormalized data with that case
we avoid a lot of joints and uh that
also increases the performance of the
query okay great so I'm done from my
side how do you have any question for me
uh no I don't have any questions and
thank thank you for your time
Ver Más Videos Relacionados
TCS Java | Spring Boot | Microservices | 4 Years | Selected | Mock Interview
Capgemini Java Developer 4 yrs interview Questions and Answers L2 round #capgemini
java microservice telephonic interview of 10 years experienced
3+ years Java Developer Accenture Interview Experience| Java | Spring Boot | Microservices
Caught Cheating - SDE Candidate interview unexpectedly terminated | [Software Engineering Interview]
Java Spring Boot 3 Years Experience Interview
5.0 / 5 (0 votes)