a Singleton Twist Interview Question with Kotlin!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Sal friends welcome back to the channel always with un Shari in this video are going to start a brand new series for Android interview question so let's get starting with the first problem so here we have this interview question and this question was asked by Microsoft I received this problems on daily coding problem you can view their website here they are great way to improve your interview skills so you can check them here okay so this is the first problem and this problem is saying the following it's saying that we want to implement single to pattern but with twist so here is the first thing so we want to store two instances instead of one instance this is the normal thing and then for every even call right for get instance we return the first one otherwise we return the second one this is the thing now we won't just solve the question because we want to share what should happen during the interview so the first thing when the interviewer ask this question we should think why the interviewer is asking me this question what he want to knows exactly so this kind of question has multiple things it has this single to patn so by asking this question let's say you don't know what is the single to patn because single to pattern is part of design patterns so the interviewer will ask you this question so he can knows whether you know the patterns or not so this is great question right so if you don't know design patterns most cases like all o developers must know at least some of the patterns or know there is something called design patterns why is that because they are so famous first knowing them doesn't mean always using them knowing them means if you fall into the same situation you can solve it with the same solution as in the design patches so this is the first thing the second thing we notice that this is kind of single pattern it means oop thing we knows that there is something related to O So the interviewer want to see whether we can hide because as you can see there is a lot of implementation going on here for even call and odd call we want to return different instances so we want to encapsulate this logic well so we can share our oop knowledge another thing related to this question is also whether you are thinking about multi-threading environments because this is a big problem when there are multiple clients requesting for instances from this single pattern right so we need to take into account this and the most important thing in all for interview question is the communication skills whether you are communicating well your solution and also whether you are asking good questions so keep in mind those things when doing your interview and part of this communication is ask for more detail so the first thing you may ask him well do I need to write test for that or I don't need to write test this to represent that you are proficient in test and you can write test based on the interview answer right so always start by communicating so I will act as I'm communicating to you which is your your my interview so if you want to implement this single to PN of course you are going to have a class definit and since you are in coton you may acknowledge something you know about coton usually in coton you can Implement directly the single to patn using the object okay we can name it Singleton like that and then we can have a function that can return an instance this is shows to your interviewer that you know basic things about C so let's start by implementing the class okay and let's make it Singleton and then we want to implement this methods so we know in C we can't do directly methods. get instance like that we need to create them as part of companion object so we need to create that companion object and then we will have a function called get instance by which it will return us an instance of singlet like that and here we are going to do just a to-do like that so we are trying to create the public API first always try to communicate with the interviewer so the first thing about Singleton we know is that they have private constructure so how we implement the private constructure in in got you can do it like that you can create private and constructure like that that would be great thing to do that's the first thing and then we need to check here we have two instances right instead of storing one instance we store two instances and you can do that in cotl immediately right you can create private ball like following you can call it first instance here also the interviewer try to see the style of coding instead of naming it a for example we are naming it proper names as you can see so we can name it Singleton like that that's first thing we need to do the second thing which is second instance so he can see uh the proper coding style but you are doing an error here which is initializing directly this instances usually we don't do it immediately we do it only when we want its value we should create it lazily okay you can ask the interviewer whether it is fine like that maybe for this kind of interview it's fine at least you want to know that you are thinking about this lazy initialization okay you can do it in C in simple way you can do it lazy like that and same thing for this one okay that's lazy initialization right always try to communicate okay now we need the thing to know whether we are in the first call or in the second call so we need to catch this using some kind of counter so we can name some kind of counter and this counter well can be zero the beginning and then here we can return it based on that counter if that counter is for example even are going to return the first instance which is first instance right we can do the return here directly else we are going to return the second instance okay that's pretty fine now as you can see here this won't work because we are using directly this thing we should have to use value here right but we can use the buy keyword here which is this one can use the buy so we can get the instance direct but and here of course you need to update this counter let's say you just forget because we usually forget so we need to test this program right always try to get feedback on your code immediately right so you can create a function here that's called output like this is function of our Singleton I'm going to print only the instance of this Singleton this is thing will print me kind of the address right so I can call this multiple times so what I can do I can do singl time like that get instance and here I can do output I can do it four times to check that I'm calling the right instance every time so here and here I should get the same value I can start running my program and I can check what I'm calling I'm call calling always the same instance like this is a way to check we are calling the same instance or not so I know that my program has a problem so using this kind of feedback you are telling the interviewer that you are rapid feedback person which is a good thing so understand that there is a problem here okay we can fix this problem easily like that and you can of course after each thing we can do counter Plus+ and we can just move this here same thing we can do counter Plus+ here okay we notice that our V we need to be up to VAR and then we can check our implementation so the implementation is correct here is the first instance and here is the second instance we can give it names here when we are calling but this is sufficient to test you can always ask like this is thing always ask interviewer about his options maybe he want to prefer like passing a name for this instance basically we can have a name for the instance right you can call it for example Val name and he using that name can differentiate both instances right you can do it like the following instance and here one and two here and then we can basically we can just output name instead of doing the reference okay exactly it is working as expected and now you may ask the interviewer for other constraint you need to show him that you are aware of some problems but whether he want you to tackle those or not the first and major problem using the Singleton is the threading you may ask your interviewer the following like this is obvious you want me to do some synchronized block for these checks for the counter and stuff like that and he will ask you yeah definitely do that first thing we can do is this check and this update because we are checking and then we are updating maybe at the moment of checking there is other instance that is updating which may be a problem so how to do that usually we solve this with atomic number okay the atomic integer and then we give it zero at the beginning and here we can do the following can do counter. get and inry which is this one so we will get it and incremented and this is atomic operation either this will happen or not so this will represent that you are doing the threading here safely another thing we did it implicitly which is this lady when there is someone accessing this first instance I mean which is initializing this instance for the first time usually this lazy have mod for the synchronization you can go to Lazy here and you are going to see that there is something called safety mode the safety mode usually it is by default synchronized as you can see we are are using the synchronized lazy block here as you can see and if you go to synchronize simply lock are used to ensure that only single threat can initialize Le instance we are doing that by default right but you need to show that your interview like this is really crucial so he can understand that you understand what you are using are not just using lazy because everybody is using lazy now you understand what you are using so this is a great example to demonstrate this lazy property and also Atomic integer and we solve the problem of Singleton sometimes the interviewer may ask you for extra thing what about adding this one what about adding this one always carefully reply to his answer maybe he want to make a trap for you so you be aware of that maybe he want to ask you why we are not doing that always think about the basics of this pattern and see if it's conflict or not okay and maybe he will ask you for some refactoring can do here for example we can just remove the parenthesis like that and we make it simple like that or we can also to add enter and we can go to the return convert expression body and here you go here is the implementation of this thing so always try to respect some good clean code practices such as naming for example and also think about the abstraction when are doing interviews related to oop that's basically it for this video if you have any interview question please send us to us we'll try to cover it with if is pure Android and also simple problems like that we can solve them in cotl and I've created a Discord server in which you can post your questions see updates regarding my videos and problems like that and you can post your questions related to Android interview question in the channel for that purpose so that's it for this video thanks a lot and see you in the next videos mic
Info
Channel: Charfaoui Younes
Views: 660
Rating: undefined out of 5
Keywords: android interview, android interview questions, android development, android developer, android developer jobs, android development jobs, remote android jobs, remote android developers, remote developers
Id: fdW2tc3dFu0
Channel Id: undefined
Length: 10min 55sec (655 seconds)
Published: Tue Jul 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.