Thumbnail for Java Spring Boot 5 Yrs Interview Experience [He Answered all] by GenZ Career

Java Spring Boot 5 Yrs Interview Experience [He Answered all]

GenZ Career

31m 1s4,286 words~22 min read
Auto-Generated

[0:00]Hey guys, today we have Mehboob and he's having total five years of experience in Java, Spring Boot and Microservices. 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 Mehboob, how are you? Yeah, I'm doing fine. How are you? Yes, I'm also good. Shall we start? Yeah, sure. Okay, so Mehboob, can you please introduce yourself and your recent project in brief? Yeah, so my name is Mehboob Jailan. I'm a Java backend developer. I'm having a total of five years of experience in backend development. And my coming to my recent project, it is called Rogers communication, where we provide internet-based products to users like. Wi-Fi, streaming, home security. And currently I am working in home security team, where we provide, you know, security cameras, alert monitoring system to users. Got it, got it. Yeah, that's it briefly about me. Okay, thank you. So it is based on Spring Boot? Yeah, yes. The backend systems are implemented in Java Spring Boot. Java Spring Boot. Okay, so Mehboob, let's say your current project is taking too long to start because of heavy bean initialization, right? So my question is, how would you optimize startup time? Okay, so if it is taking a longer time due to bean insulation, then in Spring Boot we are having an option called at lazy initialization. So when we implement lazy initialization, when the Spring Boot when the Spring Boot application is starting, it will inject, uh, create and inject only those beans which are required to start the server. And the remaining beans will gets created and injected based on the requirement. So in that way, we can optimize the startup time. Yep, yep, yep, that's correct. Because use lazy initialization, right? Okay, moving to the next question. A REST endpoint is returning outdated cached data instead of fresh data. Right? So my question is, how a REST endpoint, you have created a REST endpoint, right? Now it is returning outdated cached data instead of a fresh data. Okay. So how would you debug and fix this? Okay, so if it is a, you know, which type of endpoint is it a GET or, you know, or is it a push? Let's assume a GET endpoint. Okay, if it is returning, if GET mapping is returning, you know, outdated data, I will try to check whether they have implemented with a ID with key or not. Because we can implement the caching with just using at the rate cacheable. But if we don't implement based on key, then it won't update the data. So I will check whether it is updated with the key or not. If it is updated with key, then we will I will check whether whenever we are, you know, doing POST or PUT operation, whether we are updated with a cache PUT or not. So that whenever if you use with cache PUT, whenever if there is any kind of updation happen, the cache manager will automatically updates the cache. So in that way, we can fix that issue. Okay. Okay, so, and also, uh, we should look a TTL time to live settings, right? And as you already told cache key. That's also correct. And also, uh, we should test these things with cache evict or cache manager, right? Yeah. Okay, okay, cool. Uh, moving ahead. You have two Spring Boot microservices. Okay. They are communicating via REST endpoints. Okay. One is working fine as expected. Okay. One frequently times out under load. Okay, okay. Okay. So how would you handle this situation or scenario? So if due to, if it is going timeout due to load, I will check whether our load balancer is working properly or not. If it is, you know, if the entire load is going to only one server, so it will be very difficult for that application to work. So we will check whether our load balancer is working or not. And along with that, if our load balancer is working fine, then we can check whatever the code we have written, we can analyze the code. Okay, whether we have implemented, you know, for loops with order of N square. Okay, if we, if there are there, then we need to optimize that order of N for loop so that it won't take a longer time. Uh, we can implement, you know, resilience 4J for this fixing. Yep, yep, yep, that's correct. We could use resilience 4J and we could use, uh, timeouts like we can tune timeouts, right? Yeah, yeah, yeah. And but yes, your answer is correct. Okay. Moving ahead. Um, let's say you have an API that should be publicly accessible. Okay? You have an API that should be publicly accessible, but another endpoint should require authentication. Okay. So how do you configure this in Spring Security with Spring Boot? Yeah, yes. So in Spring Security, when we are implementing, uh, or overriding the security filter chain, we can implement them. So during this, there is a option called permit all and authenticated. So whichever the public endpoints are there, so by using the request matcher, we can give that endpoint and we can do like permit all so that it won't need authentication. And for remaining all the other endpoints, it should need authentication because we will use permit all and then we can use authenticated, so that all the public endpoints don't need authentication. Yep, yep, yep, that's correct. Okay. Uh, hey guys, I want to share something important with you. Actually, Mehboob prepared from our interview preparation kit and cracked multiple interviews. We have also referred him to the multiple companies. So guys, let me tell you this interview preparation kit has four main parts. First is complete interview preparation material. It is a step by step material made by me, expert and MC's interviews. 99% of the questions are asked in interviews are covered in it, okay? Second is two real enterprise client projects, code and video recorded sessions are there, and you can add this in your resume. Third is lifetime chat support. Here you can ask your doubts anytime. Fourth is reference support. Here we help you to get referred to the top MNCs, okay? 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. You can buy just the complete interview preparation material or the full interview preparation kit with project supports and references. I have added the links in the description below. So now moving to our interview. And guys, before moving to our interview, since Mehboob is here, so we can ask for a feedback. So Mehboob, can you please go ahead? Yeah, so I have prepared by using your interview preparation kit and it helped me a lot. Okay. Actually, I'm able to crack my interview, so yeah. Thanks for that. Okay, okay, thank you. Uh, moving ahead guys. Um, so Mehboob, uh, let's say your application works locally, but fails to connect to the database in production. Okay. What steps would you take to analyze the issue and fix it? Uh, okay. So let me think about it one second. Yeah. Okay. So when we are working in our local, right? So whatever the in application properties, whatever the data source credentials we have given, we know that it will be fine. But when we are going into prod, okay? So most of them we don't get hardcore values. Mostly we'll fetch it from, you know, some either config server or some other profiles. Okay? So I will check when we are defining in the config server, whether we defined the proper credentials or not. Okay. Then after that, if we have, if we are fetching, if we are using profiles, whether we started with the correct profile or not, like we, we can have multiple profiles, right? Okay. If it is going for prod, the only prod profile should be activated. So I will check whether we have activated the prod profile or some other profiles. So these two things we can do. Uh, that things I'm not sure about it. Yep, yep, yep, that's correct. And other than this, uh, we could check SSL certificates, connection pool size, right? And logs as you have already told this. Okay. Now moving ahead. Um, Mehboob, one of your API takes too long because it loads a large data set. Okay. So how would you improve the performance? So if the API is slow due to the large data, then we can implement pagination, okay? When we implement pagination, so rather, so instead of sending the entire data, we can send the data in chunks based on the user request. Okay, in pagination, there is a we will have, you know, what is a page size and what is a page number. Based on that, we will fetch only that that particular data and we will send that one. In that way, we can avoid this issue. Okay, that's correct. Um, moving ahead. Um, let's say you have sensitive configs. Uh, maybe DB passwords, maybe API keys in application properties or application YAML, right? So the my question is, how would you secure them? Uh, okay. So in this scenario, it is the better way is we won't store actual values in our application properties. Okay. So rather than this, we will have our config server separately, okay? In that config server, we will, we will store all these kinds of values as per our requirement, okay? And none of other applications will have those, and I will use that beans, and our application, whenever they need to start, they will fetch those values from that config server only. In that way, whenever we need to update or we need to change any kind of fields, we can able to go and change in only one, one application, rather than updating in all the 10 servers. Yep, yep, yep, that's correct. Okay. Um, moving ahead. A scheduled job in your Spring Boot application is running twice in production, even though you have deployed only once.

[11:13]What might be happening?

[11:17]Okay, so are you saying about a Spring Batch processing? Um, no, no. should jobs, yep. Okay, scheduled jobs. Okay, so in that case, so I will check in by using which method I have, you know, deployed it, whether I have used, I have used fixed rate or fixed delay. Okay? In this scenario, whatever we, uh, we defined, there is a most probably there should not be a possibility of, you know, executing that job multiple times. But if we deploy by using Chrome job, we will give the Chrome command, okay? Uh, if there is anything issue in that Chrome command, there is a chance of executing that job multiple times. So I will try to fix that Chrome job, and it will fix the issue. Yep, yep, that's correct. And also, one more thing I would like to highlight. Um, we can also check whether multiple application instances running. Right? Or multiple schedulers enabled, right? And also what you have already told us, okay?

[12:30]Okay. Um, so your REST API allows file uploads, but user complain about large file failing silently. Okay. So how would you fix this issue? Okay. So in Spring Boot application properties, we can define one key value. Spring max file upload, okay? So inbuilt, I think the Spring Boot supports 1 MB file. Okay? I guess so, I'm not sure I guess, but if user is trying to upload, you know, huge size file, okay? Then we need to manually define to Spring Boot that you can allow this much of file size in your application properties. Okay? And this way we can fix this issue. Yep, yep, yep, that's correct. Uh, we can define our maximum file size or maximum request size, right? Uh, it could be done like spring.servlet.multipart.hyphen.max.hyphen, uh, something like that. Okay. Yep. Uh, moving ahead. After Spring Boot version upgradation, okay, after upgrading Spring Boot version, your custom bean stopped working because a default bean started overriding it. So how would you fix this issue?

[14:04]Okay. So, yeah. I haven't tried this one. Okay, but what my procedure will be first, I will try to go through a little bit of documentation of that new version. And I will try to check whether if any of the like whatever I have used, when the whether any of them is got deprecated or not. Okay? If it gets deprecated, then I will try to I will try to convert I will try to change them by using the whatever the newly updated one. Uh, this is one thing we can do. Uh, yeah, actually, like I'm not sure about this, like how can we fix this, but this is one thing I will do, but Okay, uh, I think you have already know the answers, but let me tell you, uh, we could use primary qualifier or disable auto configuration. Oh, that is for, yeah, yeah, let's say our custom bean stopped working because a default bean started overriding it. Right? Uh, we can disable the default bean by excluding, or we can make our bean primary. Yeah, yeah, yeah. I know this that work, but yeah, I said you already, thanks for the information. Okay. Moving ahead. Um, your API endpoint returns an empty JSON response, even though the DB has data. Okay. What would be the possible causes?

[15:39]Okay. So in possible causes, so first thing will be, um, in most of the enterprise application, when we sending data to customer, we won't send the entire data. We will use DTOs and we will send only the required data to user, okay? And we will map the, we will map them by using the actual data from DB, and we will send it to user. I will check whether during that mapping, whatever the fields we are sending to user, okay, whether they are getting actual data from DB, or they are getting set with null. That one thing I can do. And another thing I can check, in our DTO files, whatever the annotations I have mentioned, there is a annotation called, you know, JSON include non-null. In sometimes, even the JSON itself, it will exclude some fields if it didn't find the values. So I can check whether it is working as expected or not. Okay, cool. Um, so yes, that is correct. And we could also check Jackson serialization issues, missing getters. That's a basic one. I mean, Yep, yep. Uh, moving ahead. Um, in your system, uh, you have 10 Spring Boot microservices, right? And managing configurations like, um, DB URLs, feature flags, etcetera. Separately has become painful. You got it? So how would you handle centralized configuration management? Yeah, so I will implement one particular service as a config server, okay? That config server will have all the DB configurations, all the required properties as per our requirement, okay? And none of other applications will have those, and I will use that beans, and our application, whenever they need to start, they will fetch those values from that config server only. In that way, whenever we need to update or we need to change any kind of fields, we can able to go and change in only one one application, rather than updating in all the ten servers. Yep, yep, yep, that's correct. Okay. Um, so can you tell me how would you design the checkout process for an e-commerce platform? Right, like cart, payment, order, inventory update by using Spring Boot Microservices. Just answer me in brief. Uh, you don't have to answer it with too much details. Yeah, so yes. So first of all, user will lodge into e-commerce application. Then first of all, he will add the item to cart. Okay, then cart operation will get performed, and he will able to see all his product details in the cart. Then he will go to the payment options. Okay? There, uh, we will implement our payment service separately, okay? And then wherever whatever, uh, method of payment he choose, okay, by using that we will he will do that payment. Once the payment is done, I will move it to the order page where I will confirm his order. Okay? Then after that, I will move to a kind of notification service where we will send the order confirmation to the user. Hmm. Yes, correct. Okay. Um, moving ahead. Um, can you tell me what happens if two auto configuration classes defines the same bean? Define the same mean in the sense, can you come again, please? Uh, like there are two auto configuration classes, okay, that has the same bean. Okay, okay, yes, because since they will, I mean, we have by using Spring Boot application rotation, we are just doing auto configuration, right? So, but two auto configuration classes have same bean. So, like, it will work on which bean it will fetch, which bean. So, like, I'm just asking what Okay, for that one, we are having that at the primary and at the qualifier annotation. Okay, if you are having multiple means of same type, that's what happens. It will throw an error. I just want to know about that error. It will throw an error, yes.

[20:10]So yeah, Spring Boot will throw an error, the application won't start. Yep, yep, yep. Spring Boot basically will throw an error, uh, it's a bean definition override exception. Okay. You want me to tell the name of that exception. Yep, yep. Okay, yeah, it's fine. If you don't know. Okay. So what happens if no active profile is set in Spring Boot? So if no active profile set in Spring Boot, then like uh, by default, we'll be having application properties file, right? So it will fetch data from that file. If you don't have that file, then Spring Boot will start giving us errors. Okay. Okay. So tell me what happens if you define two Spring Data JPA repositories for the same entity? Uh, same entity for two repository for same entity. Okay, okay.

[21:15]Sorry, no idea on this one, yeah, it's fine. It causes bean definition exception. Okay. Yep, yep, and how can we resolve this bean conflicts? We must use qualifier. Yeah, been conflict for primary and qualifier. By using these, we can resolve been conflict. Yep, yep. Okay. Tell me what happens if you annotate a class with both Controller and REST Controller annotation?

[21:49]Okay. So, sorry. So at the REST Controller is like on top of at the rate Controller annotation. So it is a combination of at the rate Controller and at the rate Response Body. So if we define at the rate Controller, at the rate REST Controller for same class, I don't think it will create an issue. Uh, it will work as a, it will work as a REST Controller itself because at the REST Controller is on top of the Controller. Yep, yep, at the REST Controller.

[22:38]Okay. Yep. Uh, moving to some, uh, core Java, uh, things. Uh, can you tell me why, uh, is String immutable in our Java programming language? Yeah, one is security so that no one can correct the data. Another one is memory management. Because if we are defining string literals, assume I defined string literal as string S equal to Java. Okay? So if it is not available in Spring pool, so Java will create inside that pool. And then in future if I define string S two equals also Java, okay? Then it won't create Java again. The S two also will points to the same string. So there is this memory management is there, and the security because it is not easy to correct. Uh, so these two are the important reasons for immutability of string. Correct. Okay. Tell me if you want a mutable version of string, what would you use? Yeah, in string there are two, sorry, in Java, there are two possible for them. One is string builder and the other one string buffer. Both will gives us a mutable strings options, but there is one difference is string builder is not thread safe, but string buffer is thread safe. Correct. Um, can you tell me how does hash map work internally? Uh, tell me two internal working of hash map before Java 8 and after Java 8? Oh, okay. I'm not sure with that what is before Java 8 and after Java 8, but I know how it works. Yep, yep, tell me after Java 8. Sorry. So whenever we try to save the data in a hash map, okay? Now, based on our key, first it will calculate the hash code for that key, okay? And then it will, uh, it will calculate what is the bucket value for that one. Okay? Once the calculation is done, the value will goes and sit into that particular bucket. Okay. Yes, yes, that's correct.

[25:50]And the major difference between Java, I mean prior to Java 8 and after Java 8 is this, uh, tree. I mean, before Java 8, we just used to store the things in the linked list only. There were no trification means converting linked list to tree. Okay. We just optimized things after Java 8. Okay. Um, can you tell me how does this, uh, concurrent hash map handle concurrency different than then and like, we know it is different than hash map. So like, in what parameters it is different than hash map and how it is handling concurrency? Okay, so current hash map handling concurrency based on key value pair, okay? So whenever a thread is accessing a particular key value pair, okay, concurrent hash map will sets the lock on that particular key value pair only. That means each key value pair is getting manipulated by only one thread at a time. Okay?

[26:57]So that it will maintain the concurrency and thread safety. Okay. Got it. That's correct. Um, how does, I mean, how are static variables stored in memory compared to the instance variables?

[27:16]Okay. So, in in you are saying like memory saved, right? How they are whether it is in stack or heap? Yep, yep. Okay. So yeah, like static variables are related to class, not objects. So, I guess stack, I guess because heap is used to store the objects. So I guess so because like I'm not sure about, you know, memory how memory works exactly. Uh, okay. It's fine.

[27:57]Uh, stack variable lives in method area, a class loader memory, and instance variables in a heap inside objects. Okay.

[28:08]Okay. Okay. Okay. Um, do you know about the design patterns in Java? Yeah, so yes. Uh, design pattern is used to give a proper solution to a common problems. It's not the code, it is the way how you code. Okay. There are different types of design patterns like creational, structural, and behavioral. Okay. So coming to creational, behavioral design patterns, these are not what I have used. Okay. They are we are having multiple, like Builder pattern is there, Factory design is there, Abstract Factory is there. What Builder pattern does? Okay, so assume you are having a class and it is having a lot of optional variables. And when you are creating object, you want to create the object with only few variables. And if you are having only one or two scenarios, then you can create only two constructor for that one. But in future if you are getting other requirements and for each requirement, you need to create constructor, it will be a tedious task. Okay, Builder pattern will will resolve this issue. Okay. By using that Builder pattern, we can create the objects by giving only the fields whichever we wanted. Okay, we no need to create construct, each constructor for each kind of requirement. That's how it resolves that issue. Cool, cool. And, uh, what about Singleton design pattern? Yeah, so coming to Singleton design pattern, it will, uh, it will say like each class will have only one object at a time. Okay, when one object at a time, and that object will be used for in our entire application. Okay. Got it. Okay. Uh, can you tell me the difference between lazy Singleton implementation and eager Singleton implementation? Oh, lazy Singleton, eager. Okay, let in Java, lazy and eager means whenever we are starting the application, during that application itself, it will create that object, that is eager. If lazy means whenever we asked for that particular object, then only it will get created. Okay, that's correct. So mostly, I guess it will be same scenario here also. Same scenario, but you do, you know, the implementations of Singleton design patterns? We have four or five implementations like one is enum based implementation of Singleton design pattern, one is double locking. Yeah, yeah, yeah. Okay, which one is the best? By enum is the best because enum by default it's thread safe. Okay. Yes, enum is by default thread safe. Correct. Okay. Uh, let's say if someone ask you to write the code of Singleton design pattern, best way of Singleton design pattern. So what would you write? Say enum based or double locking?

Need another transcript?

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

Get a Transcript