Thumbnail for Java Spring Boot 5+ Yrs Interview Experience by GenZ Career

Java Spring Boot 5+ Yrs Interview Experience

GenZ Career

35m 28s4,620 words~24 min read
Auto-Generated

[0:00]Hey guys, today we have Navanshu and he is having total 5.3 years of experience in Java, Spring Boot and microservices.

[0:08]And guys, those who want to give same free mock interview, please fill out the form below in the description, so now let's get started. hey hello Navanshu, how are you?

[0:17]Hello Faizan, I'm good. How are you? Yes, I'm also good. So are you excited for this interview?

[0:23]Yes, very excited. Okay, okay, nice, nice. So now, Navanshu, can you please introduce yourself and your recent project in brief?

[0:31]Sure. So, my name is Navanshu. I'm currently staying in Pune. I completed my engineering in the year 2020.

[0:40]And since then I'm working in IT. So, I have around 5.3 years of experience.

[0:46]My tech stacks include Java, Spring Boot, microservices. Along with that, I've also have hands on experience on AWS services like S3, ECS, Lambda, Code Pipelines.

[0:58]And we have I have used, you know, Postman to test our HTTP endpoints. For documentation, I have used Confluence.

[1:05]For version control, we have used GitHub. Apart from that, we maintain Jira tickets, and we have been following Agile in my all 5.3 years of experience.

[1:16]I have around experience in domains like identity and access management, demand and supply.

[1:23]So, this is a high-level overview about myself, and currently, I am with Volkswagen as a senior software developer.

[1:31]Okay, okay, that's very nice, Navanshu. So, are you leading any feature as of now?

[1:38]No, not as of now. Okay. So, are you using transactional annotation in your project?

[1:43]Yes, not specifically, but since we are using Spring Data JPA, it internally uses transaction annotations.

[1:50]Okay, okay. So, can you tell me the difference between transactional annotation on a public and a private method?

[1:57]On public and private methods. So, transaction annotation, transaction is like, you know, it is a group of action, right?

[2:07]Whether it happens for all or it doesn't happen. So, basically, a transaction, if it succeeds, it should be complete if at all anything is failing, it should be rolled back.

[2:16]So, in terms of like, when we use transaction in internally in our repository layer,

[2:24]Okay.

[2:27]So, it, it is basically to achieve the atomicity, consistency, and durability of our data into the database.

[2:36]Right. But if you specifically ask

[2:43]Yeah, but if I put transactional annotation on private method, what will happen?

[2:48]So, if you put transactional annotation on private method, transactional annotation on private method.

[2:57]Will it work or not?

[3:00]It should work, but I'm trying to figure out.

[3:05]Okay. Yeah, it won't work. Because transactional annotation works on proxies and all, and proxies don't work on these private methods, right?

[3:19]Okay. Yep, yep. Can you tell me the difference between authentication and authorization?

[3:26]Yes, authentication is like proving your identity. It is what you are. And authorization is once you have proved your identity, authorization is whether you are authorized to access that.

[3:38]So that is a very basic difference between authentication and authorization.

[3:41]Okay, cool. Can you tell me how does Spring Security's filter chain work?

[3:48]Spring Security filter change in terms of like what?

[3:52]Have you used the filter security filters in your Spring Boot application?

[3:58]No, basically, we are using OIDC and JWT for authentication and authorization.

[4:04]Okay. So, how JWT works?

[4:07]So, if I tell you an entire flow, so as soon as a user hits our URL, it first comes to our application.

[4:17]Our application is configured to, you know, communicate with the IDP, which is your identity provider. First, it checks the request that session exists for that request.

[4:30]If that session exists, then a single sign-on is successful. It is allowed to access the downstream services. If not, the request is routed to an external IDP, where external IDP will validate that request.

[4:47]Will sign the JWT using the private key of the .p12 file, and that signed JWT comes into our application, right?

[4:56]Then our application, what it will do? Our application has the signed JWT. Now it will make a call to the backend service. Before making the call to backend services, interceptor will intercept it.

[5:04]Clone that request, attach the JWT as a header, basically the bearer token, and then it will flow down to your backend service. Backend service is configured as a resource server. So, what it will do?

[5:15]It will fetch the role. So, in our JWT, the main claim is the user's role, whether it is a logistician, is it a buyer.

[5:23]Depending upon that, we will fetch the role and then we will allow the access of and will pass it as a header so that the request can be flowed through downstream services.

[5:34]If at all the authentication fails, then we give the 401 error. If not, then the request is allowed to flow to the downstream services.

[5:43]And I can also tell you that how our backend validates whether the request is correct or not. So, with the help of the public key, it will make a call to your IDP via JWKS.

[5:55]And then it will identify whether it is authenticated request or not, and then it will provide the access.

[6:03]Got it. Hey guys, I want to share something important with you. Actually, Navanshu prepared from our interview preparation kit and cracked multiple interviews.

[6:10]We have also referred him to the multiple companies. So, let me tell you, the interview preparation kit has four main parts.

[6:18]First is complete interview preparation material. It is a step-by-step material made by me, experts, and MNC interviewers. 99% of the questions asked in interviews are covered in it.

[6:27]Second is two real enterprise client projects. Code and video recorded sessions are there, and you can add this in your resume.

[6:34]Third is lifetime chat support. Here you can ask your doubts anytime. Fourth is referral support. Here we help you to get referred to the top MNCs.

[6:42]So, basically, the material is organized as per your experience level and covers Java, Spring Boot, Spring Security, Spring Data JPA, microservices, Kafka, Maven, Git, coding questions, stream API coding questions, and many more.

[6:53]You can buy just the complete interview preparation material or the full kit with project supports and referrals. I have added the links in the description below, so now moving to our interview.

[7:01]And guys, before moving to our interview, since we have Navanshu here, so we can ask him about feedback. So, Navanshu, can you please go ahead?

[7:11]Yeah, so, guys, please check out that material. It is awesome. It covers almost every question interviewers ask nowadays, and do check it out.

[7:22]It's great stuff there. Okay. Thank you. Um, so now moving to our interview. So, Navanshu, can you tell me how does Spring Autoconfiguration work?

[7:33]Under the hood. Yeah, so depending upon the dependencies that you have added in your pom.xml file, and when you run your application, a lot of depending upon that dependencies, Spring autoconfigures stuffs for you.

[7:48]There is a classpath and it takes the reference from that pom.xml file and lots of dependencies are available. Depending upon that, it will autoconfigure stuffs for you.

[7:59]It will autoconfigure different classes, libraries, basically everything that you need to for the configuration purpose of your project so that you focus on your logic building, code, your business rather than worrying about the configuration and stuffs. So that's how it happens.

[8:15]Correct. Um, how does Spring decide which beans to create internally?

[8:21]So, it scans, so we have annotation, okay, Spring Boot Application annotation. So, it internally has component scan.

[8:33]So, what that component scan will do? It will scan all the annotated beans, annotated beans, and those annotated beans would be registered into your application context, basically Spring container.

[8:41]In this way, it can it decides like which all beans to be considered while, you know, while the Spring Boot application runs.

[8:50]Got it, got it. Can you explain the Spring Bean life cycle from instantiation to destruction?

[8:57]So, okay, life cycle. So, life cycle is from right from the Spring creation to the Spring destruction.

[9:08]So, as soon as your Spring Boot application runs, the run method gets triggered. When the run method gets triggered, environmental properties get loaded, and depending upon that, your application context gets created.

[9:17]Right, and your annotated beans get registered into the application context. By default, the scope of the bean is singleton because the default scope of a bean is singleton.

[9:30]So, as soon as that happens, that is the bean we can say, the bean getting created. Now, as and when the beans are required, we have like constructor injection, setter injection, field injection, depending upon whatever you have used.

[9:44]It will automatically inject those beans into your system. Then you can, you know, play with it.

[9:52]You can like depending upon the scope, you can change the scope, make it prototype or session or application. Depending upon the requirement, you can make the scope. And that's how you will be able to mold the, you know, beans as per your requirement.

[10:05]And once the, you know, it's use is over, probably possibly like if we can say it's purpose is served, then it's like, that's the basically the life cycle of the bean, we can say.

[10:18]Okay, okay. Do you know about Post Construct and Predestroy annotations?

[10:23]Yes, yes. Please go ahead. So, at the Post Construct annotation is basically used when you want to configure something at the time of when you want to configure a bean at the time of the application startup.

[10:41]So, anything you want to, you know, configure right at the time of the Spring Boot application start, you put it into Post Construct. Example, if I'll give you if you want to, you know, whenever your application, you want when it starts, you want default values to be loaded into the database, that code you can put it in your Post Construct.

[10:56]It is as good as an interface we have one interface called Command Line Runner, right? It internally has run method, right? So, it is as good as that.

[11:06]Whatever you want to run at the time of application startup, you put in that run method, which is implemented by your main class. Main class means the class which is annotated with Spring Boot application.

[11:15]So, it is as good as using that. So, anything you want to run at the application start up, you put it in Post Construct.

[11:24]Okay, okay. So, since we use some injections like constructor injection, setter, getter, oh sorry, field injections, right?

[11:35]So, my question is why is field injection considered bad practice?

[11:41]Okay, so field injection is because it is very difficult to, you know, it is easy to write but it is very difficult to test.

[11:50]Generally, if we prefer constructor injection in production grade codes because first of all, it serves very important three things like it is immutable, right?

[12:02]The, it is compulsory, right? And when you write code, you need to write unit test cases for that.

[12:08]So, writing, you know, it helps us with testing. You can easily, you know, use it while injecting that object while writing the test cases.

[12:19]So, basically, it is easy for testing as well. That's why we prefer constructor injection over any other. Yeah.

[12:26]Correct, correct. Have you faced circular dependency in your career?

[12:31]I have not faced but I am aware about those dependencies where one dependency.

[12:37]So, how can we resolve it? Using setter injection, we can if you are talking about bean wise, then we can use setter injection.

[12:47]Or else on the another level, we can make like we can we have an annotation called as if like Qualifier we can use, or we can lazy load one of the dependencies so that the other dependency is picked up first.

[13:02]These are few of the ways where wherein we can solve the circular dependency issue.

[13:07]Got it. Um, so let's say your Spring Boot REST API is experiencing slow response time under load. How would you investigate this issue and fix it?

[13:19]So, first of all, my first thing would be I'll go on and check the CPU utilization. I'll check the logs. I'll identify the root cause.

[13:30]Then depending upon that, I can apply, like, I'll see the number of requests hitting my application. I can apply rate limiting there.

[13:39]Then I can apply pagination. Apart from that, you know, in the code level, I'll try to write optimized code.

[13:47]See, the main thing that makes your response slow is your database queries. I'll check whether the queries are optimized or not.

[13:54]If not, then I can use indexing. If I'm seeing that, okay, the query is, you know, written such as select star.

[14:00]I will check like what all columns are actually required. I will consider those. So that is that is me optimizing the queries. Then on the database level, I can check.

[14:10]We can make things like I can use Master-Slave approach, where all the write operation happens at the master database, and all the read operation happens through the slave. So, so that the load on the database decreases.

[14:21]Apart from that, we can introduce asynchronous programming. So, these are few of the ways I will improve. We can introduce caching.

[14:28]So, can we utilize Spring Boot actuator endpoints?

[14:34]Yes, yes, yes. We can use that as well, because Spring Boot Actuator's health endpoint can give us the status regarding the database connectivity, its threshold values. We can consider that as well. Yeah.

[14:48]That also gives a very important insight. Yeah.

[14:51]Okay. Okay. So, let's say you need to expose a paginated filterable REST API for a product catalog with millions of rows.

[15:02]How would you design it? So, I can set up certain pagination, we can make use of, see, Spring has provided us with, you know, pagination. So, we have pageable interface, right?

[15:17]So, we can always define the number of page, its size to be displayed. So, we can implement that. We have pageable interface, and we can make use of, we can always give the number of pages that we want to display.

[15:32]So, no matter how many requests we get and no matter how many data we have, we will always, you know, our endpoint will always give the set number of data. So, with the help, basically with the help of pageable interface, we can do that.

[15:47]Okay, can do that. Correct. And also, we could use index columns for filterable fields.

[15:53]Yes, yes, yes, yes. Okay. Have you encountered N+1 query problem ever?

[16:01]Okay, I have not came across that, but I know what it is and how to resolve it. So, like, if I tell about the problem, so it is like if you are calling one query, which is internally calling, let's say 10 other queries.

[16:13]So that is N+1 issue. So, like if you are fetching an order based on order ID, but that order internally has 10 other products associated with that order. So that is N+1 query. You are calling one query, but it is internally calling 10 other queries. So that is N+1 issue.

[16:30]So, in the like the most preferable way to, you know, fix that is using fetch join. So, with that, that issues can be solved.

[16:38]There are I'm sure there are other issues at as I mean other ways also we can solve it, but using fetch join is most preferable. So, yeah.

[16:47]Correct, correct. Okay, so let's say your you are building a Spring Boot application that must support both SQL and NoSQL database, means MySQL and MongoDB, depending on the environment. How would you structure your Spring Boot application?

[17:03]So, when we talk about SQL, so it is more structured. It follows certain schema. So, when and it helps us with complex joins and queries.

[17:16]So, for certain important tables, like let's say our database containing the users, product related, I will have those data stored in MySQL table.

[17:27]But for unstructured data, if I, let's suppose, if I have to create a notification service.

[17:33]So, notification table, user notification table, like mapping, like if you are the user, your notification should be specific to you. I will not be able to see it.

[17:44]So, we have notification table, we have user specific, user mapping notification table. So, for that unstructured operation, so I will have those as, you know, NoSQL database. So, this is way I will plan.

[18:00]Okay, okay. Okay, so moving ahead, do you know about CAP theorem, CAP theorem in microservices?

[18:11]No, at the moment, I'm not.

[18:15]So basically, CAP states that a distributed system can only guarantee two of the three. And what are three? Consistency, availability, and partition tolerance in the microservice architecture.

[18:33]Okay. Okay. So, can you tell me the difference between synchronous and asynchronous communication?

[18:39]Yes, so synchronous communication is like when one microservice wants to communicate with another microservice, it sends a request. But the first microservice will wait for the response that is coming from the another microservice.

[18:54]This means we are waiting, and then only it will proceed for the next operation. But with asynchronous programming, or sorry, asynchronous communication between microservices, it's like you don't wait.

[19:02]You send the request, you don't wait for the response. You go on, carry on with your work. The another microservice can decide on its own term when to, you know, give get back the response.

[19:16]So, it is more loosely coupled. So, yeah, that's the basic difference between synchronous and asynchronous. Okay.

[19:23]In Saga pattern, what is choreography and orchestration?

[19:28]So, it is the two ways how you implement Saga. So, orchestration is more centralized way.

[19:38]So, we have one orchestrator service. So, microservice, let's say we have three microservice. Microservice one, and orchestrator act as a central communication channel between all these.

[19:49]They don't communicate with each other, they communicate via the orchestrator service. So, if suppose, one service completed its task, it will, you know, notify that orchestrator service.

[20:01]Orchestrator service then will ask the second service to perform its task. If any of the microservice fails, then a compensation service is run which will roll back the transaction in the same order in which it happened.

[20:13]Similarly, if you talk about choreography, so choreography is event driven. Here we don't have any centralized, you know, orchestrator like we have in orchestrator service. Here it is event. So, one service will produce an event.

[20:26]Let's say order created. So, payment service will react to that event and start its processing like payment processed. But if suppose, like order service has, you know, put an an event like order is unsuccessful. So, in that way, the payment service will not process.

[20:47]So, it is basically event driven. We here we don't have any centralized orchestrator to handle it. So, that is choreography.

[20:53]Okay. So, do you know the difference between continuous delivery and continuous deployment?

[21:00]Difference between those?

[21:05]Continuous deployment is like that we do frequent releases, like when we in Agile we, you know, do epic based deployment, like deliver as far as possible. But I'm not sure about the exact difference, theoretical difference between CI and CD.

[21:23]So, a continuous delivery is the initial step of the continuous deployment. First step is continuous delivery, then continuous deployment.

[21:32]In continuous delivery, every change that passes automated test is in a releasable state and can be deployed to production at any time.

[21:40]Okay.

[21:46]Okay. So, let's say you are asked to design a URL shortening service. How would you do that?

[21:53]So, URL shortening is the basic idea behind that is we want one table, which is going to hold your actual URL, your shortened URL and there will be one ID.

[22:10]And that ID would be used to convert those to short and back to normal. So, this is the basic idea and the logic behind URL shortener, and that way we can design that.

[23:12]Got it, got it. Okay, so can you tell me can a static method be overridden?

[23:16]Static method overridden?

[23:19]No, no, static method cannot be overridden. Static method, it belongs to class. Overridden, so it's it cannot be overridden.

[23:31]Okay. Okay. So, can you tell me what is a double checked locking pattern for singleton?

[23:36]So, it's basically, so in singleton class what will we do? Like we.

[23:48]I will tell you the purpose behind that. We, whenever we are creating the object, right?

[23:55]So, first we'll initialize one object, right? We'll have a private constructor of that singleton class.

[24:03]But before that, we will check if that object is null. Inside that if loop, there would be we'll create one synchronized method, and inside that synchronized method, again we will check that is a double check wherein we are checking if that object is equals to is equals to null.

[24:17]If it is not, then we are creating the object. And like why we do it is the question.

[24:30]Synchronous we do because we don't want multiple threads to be accessing those and creating the object for that.

[24:40]That is the reason why we do synchronize. And one first check we do because we.

[24:45]So, do you know the best way to implement singleton design pattern?

[24:50]Best way is I think we to implement it using enums. Enums.

[24:55]I think. Yeah. Yes.

[25:01]But I haven't implemented it, so I have. I think enum is a thread safe something like that by default when JVM calls.

[25:10]Yes, I have read about it that best way is through enum.

[25:17]Okay. Okay. Let's say there are two integers. Integer A and integer B.

[25:27]Okay. Both have a value 200. Integer A is equal to 200 and integer B is equal to 200.

[25:35]Now when will you compare those variables? If A is equal to equal to B. So, so what will they return? It will be false or true?

[25:47]So, it is the first like first question is like is that integer, INT integer or it is a like wrapper class?

[25:54]Wrapper class.

[25:57]Okay, it's a wrapper class. Okay, so if it is a wrapper class and you are checking, it will give true.

[26:06]Because what I know is like for int, it is the range is from minus 127 to plus 128.

[26:14]It is beyond that range, but this is a wrapper class we are using. So, with wrapper class, I don't think we have that range. So, if 200 it should be.

[26:24]It will return false because wrapper class check, I mean double equal check the reference, right?

[26:33]Yes, it checks the reference. Okay, so I was just getting confused whether it the cache range is for the primitive type or the wrapper class type. So, it implies for wrapper class as well, right?

[26:54]Yep, yep. And if the if the values are 100, 100, A is equal to 100, B is equal to 100, then then it will be true.

[27:01]True. Yep. What happens if you call a non-static method inside a constructor and the method is overridden in a subclass?

[27:11]When we call static method inside a constructor, when we call a non-static method inside a constructor, and the method is overridden in the subclass.

[27:26]And the method is overwritten into subclass. So you have a parent class, and inside that constructor, you are calling a method, right? And that method is overridden from the another class.

[27:41]In a subclass. In a subclass. Okay, okay. So, first thing is in the subclass, whenever you will create I mean, the main question is which method will run? Same class method or overridden method in the subclass when we call from the constructor?

[28:06]Overridden method in the child class that will be called first. Why?

[28:13]Because it is like we can say it is runtime polymorphism we called it. So it will first check the child if it is there then it will consider that if it is not there then it will go to the parent class constructor.

[28:33]So first it will check the child.

[28:36]Correct. Can you load the same class twice with different class loaders?

[28:46]No, because a class loader loads the class into JVM so that it is into the memory so that it is made available for JVM to use. But if you load it again, no, we cannot. Answer is yes, the same class can be loaded multiple times by different class loader instance. But I think answer is no for the same class loader something like that.

[29:10]Okay. Okay, so for different class loader, yeah, okay. Do you know what parent delegation model?

[29:16]No. No. Okay. So, when a class loader is asked to load the class, it first delegate to its parent class loader, means it first asked to its parent's class loader.

[29:27]Only if the parent cannot find the class, does the child attempt to load itself, then it will come to the child class loader, something like that.

[29:38]Okay. Okay. Okay, so let's say you need to design an immutable class that contain a mutable object like a list. So, how would you how would you design that?

[29:50]So, as you say, first of all, if I have to design an immutable class, so I will make that class as final so that no other class can extend it.

[30:01]I will have attributes as private and final, private because the attributes should its range should be within the, you know, class, final because we don't want to, you know, we don't want it to change.

[30:13]We will not have any setter method. We'll only have getter methods, and we will have a constructor, parameterized constructor. Now you say that we have a list.

[30:23]So, how can so in a getter method, instead of returning the actual list, I'll return the clone of the list. So, any changes that happens that happens to the clone copy.

[30:35]The original copy is unchanged. So, that's how I will, you know, deal with the list.

[30:43]Correct. Can a constructor be private? Yes.

[30:48]Okay. Can constructor be static, final or abstract?

[30:58]No. Because we need constructor to create the objects of those class. So, it it cannot be like static.

[31:11]Yep, yep. Final also. Yeah, yeah. Okay, so moving ahead, do you know the difference between process and thread?

[31:59]Yes. So, process is thread, you can consider as the lightest, you know, substance that is able to do a work.

[34:17]And a process contain a, you know, thread to function you need process. So, so inside process there would be thread, there can be multiple threads inside that process. So, a thread needs process for its functioning and process is like it inside an operating system, there can be number of process which would be, you know, doing the job for you.

[34:41]Okay. Have you faced dead lock situation in your project ever?

[34:49]Not quite, but I'm aware about the concept.

[34:53]Okay. How can we get rid of deadlock?

[34:59]So, like deadlock is like when one of the thread is depending on another thread which in turn depends on another thread. So, we can, you know, make use of, you know, methods like wait, thread.wait, thread.sleep, then we can notify and, you know, re-spawn the, you know, sleeping threads. So, in that way, we can make the establish the communication so that deadlock situation doesn't happen.

[35:27]Okay. Got it. Okay, Navanshu, so yeah, it's done for me and it was a very, very nice interview.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript