# Quant Dev C++ Interview Question: Implement Shared Pointer

## Метаданные

- **Канал:** Exponent
- **YouTube:** https://www.youtube.com/watch?v=-Sf_lMpppQg
- **Дата:** 05.05.2026
- **Длительность:** 43:20
- **Просмотры:** 1,055
- **Источник:** https://ekstraktznaniy.ru/video/49995

## Описание

Watch SWE mock interviews, practice questions, and prep for upcoming interviews. Start free: https://bit.ly/42iyHqi

Shared Pointer is a common quant developer interview question.

This interview features Zack Light: 
https://www.youtube.com/@zacklight 

👉 Subscribe to our channel: http://bit.ly/exponentyt
🕊️ Follow us on Twitter: http://bit.ly/exptweet
💙 Like us on Facebook for special discounts: http://bit.ly/exponentfb
📷 Check us out on Instagram: http://bit.ly/exponentig
📹 Watch us on TikTok: https://bit.ly/exponenttiktok

ABOUT US:
Did you enjoy this video? Want to land your dream career? We are an online community, course, and coaching platform to help you ace your upcoming interview. We have helped people land their dream careers at companies like Google, Microsoft, Amazon, and high-growth startups. We are currently licensed by Stanford, Yale, UW, and others.

Our courses include interview lessons, questions, and complete answers with video walkthroughs. Access hours of real int

## Транскрипт

### Segment 1 (00:00 - 05:00) []

and you interview be like, "Wow, this guy really knows what they're talking about. " Hi, welcome. I'm Zach. You can find me on YouTube at Zack Light. Today, we're going to dive into very common interview question for quev candidates. Basically, if you are coming out of school applying to say like tier one, tier two companies or if you are experienced candidate, you can pretty much expect to get asked this question. The question is shared the pointer. There are actually not a lot of resources online in terms of the a really good quality implementation of STL data structures like shared pointer. Most of the implementations you can find online, they sometimes would have bugs here and there and they don't have they don't use the best practices such as copy and swap that I will talk about in today's video. Before we dive into the actual implementation, I just want to make sure that you have the right understanding for what it is. A short pointer is essentially reference content smart pointer. Think about how typically use C++. If you allocate an object on the heap on like the free me free store memory, you basically also supposed to free that allocation yourself otherwise it would be considered memory leak and if you keep doing that you would your system will run out of memory. So smart pointers essentially is just a way for you to do the allocation but have the language itself manage the lifetime of the objects that you allocate on the free store or on the heap. The free store is basically like the actual generic term for heap because the when you do you know when you do like new type t invoked constructor it doesn't the standard doesn't say has to be on the he just says should be on the free store. I'm sure a lot of things that you can say and you can do to impress your interviewer today. So maybe like you know the first one would be free store right what's the difference between free store and heap it's this is kind of like pretty trivia so probably most of the interviewers won't really know what a free store is so yeah just teach them a few things and then they would be begging for you to come join them all right so smart pointers right basically they they're safer because they will take care of deallocating the object for you and now what is reference counting? Reference counting just means the way that the shared pointer works is by counting how many references that you like your current program has to the underlying resource. So for example, if you have auto shared pointer equals I'm just going to do um right. Usually you do something like this. and then give it your custom data type. I'm just going to use T here. And then like you pass whatever this SP is like one copy of the reference. And then like if you create another variable SP_2, you now essentially have two copies. And then the reference count would be two. So the share pointer would manage would keep track of how many copies your program currently has or how many references copies of references your program has to the underlying object right so here you can do something like you know new like int 30 right or like a heavier object basically now you have two copies two references to the 30 that you allocated on the heap right and then at the If say if like this is a scope and at the end of the scope because the you know automatic storage so they would be deallocated right so I guess tip number two is here for you it might be nice to spend time studying like the types of storage I think they can be very extremely impressive to kind of distinguish between the different types of storage in an interview right what is automatic and now the end of the scope here these two variables and autocope and they get the allocated so or like the destructor would be called and in that case the smart pointer would be smart enough to know oh so nobody actually you know is referring to the int of 30 that I put on the heap earlier or free store earlier so now it's a good time to go and free it up so basically at this point the smart pointer. Now that you know what it is, I

### Segment 2 (05:00 - 10:00) [5:00]

think we can just dive into the actual implementation and I might ask some conceptual questions along the way in terms of like C++ trivial preparations. The other thing that you could talk about if you ever get asked what a shared pointer is how it differs from the other types of like memory manage automatic memory management in say like Python or like C right in those languages like the language they would often do both reference counting as well as like kind of like the scanning of like the entire memory map for you. Why? Because imagine if you have a case if like this spa points to SPB and then SPB points to um spa back, right? Or oh or if you have like a third one, SPC, B points to CC points to A. But whenever you have a circle that point to each other like even if you're out of the scope like the destructor will see that oh SP something else still points to SPB and the SPB will see something still points to SPA. So basically it's kind of like a deadlock situation really when you have this kind of a situation basically like the system the language itself won't be able to handle the free the allocation for you easily. So that's why even though C++ now has this kind of smart pointers you still need to be very careful and not to basically like create like you know cyclic references. you have to be like very careful when you use them. The reason why there's no scanning in C++ is because like as far as smart point goes like it's much lower overhead. It's a lot more deterministic. U sometimes the program would have a word pause and then like the language will scan all the memory. It's going to take like a lot of milliseconds to do so sometimes even like second at the second level. So it's not good for you know a language that needs to be heavily used in low latency programming. For example, you know, in the world of quantitative trading, hopefully I didn't take too much time to just talk about SH pointer. Yeah, let's talk about the code. Now, it's important to first have a good understanding of like how you might want to implement it, right? Essentially, all the SH pointers that point to the same thing, they need to basically keep track of how many copies are left and then they need to like share this like count, right? So the logical way to do it is to have a location in memory that you want that has like counts of the number of references and we count I'll talk about in a bit. So each shared pointer implementation basically each instance needs to have two pointers one to the actual object and another to the control block. I know someone might ask you in an interview like why can you not just like put the pointer to the resource inside control block. The reason is that the standard has certain functions that would require you to have it in the instance. So we can talk about that later in a bit. And then just like vectors you can have like a allocator and just like unique pointer you can have a custom deleter which for in the case of short pointer like would need to be stored you know in this control block. Yeah. And that's why here you can see how we have an underlining pointer that is basically the T is a template type and underlying pointer would point to the resource corresponding to this one and then the pointer to control block would be this one. Yep. So I don't use underscore myself just because according to the book clean code ID is good enough to distinguish between your local variables and your input. I think I should change them to you to use different colors. I just haven't configured them to do so. So that's probably a really good idea. Yeah. And then here you see in the control block, we're going to we're not going to worry about delet is almost certainly out of the scope of interviews because it's just like type eraser stuff and on top of implementing a smart pointer is like going to take probably more than an hour. Uh so that won't happen and we're just gonna but it's still good to bring it up in your interview right so that's why here I'm saying like oh just to do optional you know deleter so I'm going to keep the list of things you should talk you can talk to appear to like a really advanced correct C++ dev in interviews so one thing you can talk about is like the deleter the virtual function inside and the type eraser invoked this one is pretty advanced so

### Segment 3 (10:00 - 15:00) [10:00]

like I would not expect any candidate I would not like require any candidate to you know have to talk about it. Um the other thing is sure the pointer needs to be threat safe. Uh the underlying resource itself does not need to be threat safe. It can just be a struct you know type that you create but the sharp pointer itself by the requirement of the standard needs to be thread safe. That's why we will deal with atomic types as well. I actually recommend always naming the shadow pointer or name like the STL data structure you asked to implement like with your own name as the prefix just because you know you want to avoid the name collision first, right? And then secondly, you want to basically kind of like demonstrate that you are not cheating just because the individual would ask you to implement something and then you know whatever cheating software people use, I imagine they would just spew out like a standard implementation. But if you're able to use like a prefix for your class and have to refer to it, that would just help demonstrate that you're not copying the code from like you know another software. Because this is a solution walk through video, I'm not going to be typing like you know line by line because that would take too long. I will just refer to my notes and basically give you the most important things like explain how it works and then explain what things you can talk about in an actual interview to impress your interviewers. Let's look at the constructor. Basically, because I initialize the values at a point of decorations in memory initializers, I basically can no, you know, I don't have to worry about like the actual default values when I have multiple constructors. As you can see here, the first constructor, default constructor that takes nothing. I just default it, which would just set those to no. And here, this is a cool thing that I learned. If you can basically like overload it and then you these two functions are overloads right? So if you take in like the no pointer type you can skip the actual variable name since it will not do anything with it. Basically just means that if the compiler knows that at the compile time it knows this instance is just taking a no pointer it would you would know determine the compile time to invoke this one. Uh and then you would see oh I don't have to do anything. So like it's be it's you won't have to go in here and then do the if check and all that. So actually technically because we are doing the no pointer check here I think we should probably be fine with like removing this check if there's actual pointer only then we would allocate the control block on the heap right this one will just save us some time and less overhead and you see how do explicit here just because otherwise like a pointer might like be implicitly converted even though it might be a type in a program and then there's also no except right so as a general rule I would say that no except explicit and non no discard is like optional in my opinion but const uh would be required like say if a function is const I think people do expect you to kind of like recognize and then type out the word const. Yeah, you're just a word of interviewers. I guess if I can decide it like usually the IDs will tell you something's const um so if the team has a good you know ling mechanisms for the code pipeline you should be able to catch it. The other thing you should you might want to talk about or you should know is the sh pointer unlike the counterpart unique pointer does not have a version that takes in like an array of pointers since we already doing we already you know dealing with overhead of like memory allocations on the heap. So we don't actually want to worry too much about like the uh like the low-level overhead. So in this case we might as well just do a vector of share pointers. Why? Because you know like vector has a little bit of overhead. Un pointer could get away with it but you know but for sure pointers like because we already have overhead some overhead already we cannot get away with it. So like now at this point we might as well just favor composition to uh inheritance. But really there's not nothing uh inheritance on that part. But really just like a favor conversation for that point. Now let's think about the rule of five. Right? Rule of five everybody would expect to talk about in an interview. Rule five, rule of zero, rule of three. For rule of five is just

### Segment 4 (15:00 - 20:00) [15:00]

it just means that if you have one special member function, you should define like among the five special ones, all the other four as well. Um and then when we talk about this the five one in the rule we're talking about the move constructor copy constructor move assignment and copy assignment operators as well as the destructor. Hopefully yeah you were able to remember which what are five special functions right because like if you can't answer that like the interview might just be dumb at that point. Sometimes when I ask people what are five what are five functions in rule five they say oh is one of them would probably be the constructor. No, constructor is not in there because you can still have custom logic in the constructor but rule five is really about resource management in this case because we're allocating memory. So in the destructor we have to potentially deallocate it, right? So we have to have a custom destructor. We cannot just use a default one. That's why we have this special member functions as well. I'm not going to go over this. I think this is pretty standard copy and move. But this one is interesting because we actually you see how we like we're taking the parameter by copy. So and I only have this one single function for the assignment operator. So you would actually match both the copy assignment and move assignment. Here I'm doing the copy and swap idom. The reason I'm doing copy and swap is that it's just so much easier compared to not doing. So let me first explain how it works. Basically I take this other by copy right and then I swap other with my own myself my own instance. This is the member swap function. it just basically just like swaps the you know values of this two variables right so basically this temporary that I just copied will be the new me it's like I just made a copy of you know whatever the user passed me right so that's exactly what this assignment is about and at the same time whatever I had would be swapped to this temporary other which will go out of scope here and be properly destroyed accordingly. So that's the beauty of the copy and swap EDM. If you look at the alternative of not doing so first I have to you know have one that's just for the copy assignment right I have to do a check to see if you know well the usual way is to do a check to see if like you know the other and the self is the same if it's the same then we just do a return but in the carbon swap EDM actually like it's pretty common not to do the if check just because usually self assignment is very rare And it this does handle the self assignment gracefully. So by not having to check we essentially can get a smaller object file we remove a branch in the control flow which would result in actually more effective instruction prefetching caching and pipelining. So that's the advantage of not doing the if. But when it comes to the move assignment operator, the temporary one will indeed have to do the you would increment the counter and it will be decremented. But you can see here in the copy assignment you actually you know some people do the check and then you have to make sure you release whatever resource you had. Then you copy from other right and then it's the same you have to release and you have to copy from other but here is just much simpler to write pretty neat for interviews destructor just like potential release it will get to the tri release in a bit and then this these functions are simpler yet we just return underline pointer boo you can just check to see if it's no or not I did explicit here also to prevent implicit conversions this really is up to you as implement of your data structure whether you want implicit convergence to boo or not the the reference operator right here another bonus point in interviews is to basicize a little bit like other statements because other still stripped out in release build so if you are like you know running tests right statements can be quite nice to catch bugs because otherwise you just get sick foot and all bets are off so now here's a question what if you create a should pointer and then it is indeed a no like you didn't pass any pointers just like default constructed and then you call the reference on it right of course it's going to sack fault like does the standard should the standard be throwing an exception um no because the standard wants to keep the overhead of common operations really low even if it's a bit dangerous it trusts the user to do the correct checks beforehand like the bull conversion got it Then the arrow

### Segment 5 (20:00 - 25:00) [20:00]

operator right is the same. The arrow operator is like super interesting. Maybe we can talk about this in another episode. Basically, there can be some recursion going on like you can run into like an infinite loop at the compile time just by not returning a pointer type which is messed up. It's pretty messed up. Uh the comparison operator or compare underline pointer get count. This is super important. uh I made this mistake of not checking whether I have underlying resource or not and because I made the b conversion explicit I here I'm just doing a static cast to boo which is an explicit conversion to b and this would call the if I do a go to right oh actually didn't work but it would go it would call the b operator above the alternative is to actually invoke the b operator manually I have example Here it's super uncanny because you as you can see here there's a space guys in the middle of your function to invoke this operator manually so I prefer static cast it's more explicit people understand it better anyway it's important to know that it does not mean you will have zero runtime overhead obviously you will call functions when needed which will be runtime cost like I can do arbitrary stuff in my B operator. The standard actually does require you to return zero if you are not to manage any resources. Otherwise, you can just go to the control block and return the number of reference counts. So that's in the control block. Then reset we just call try release. When you don't pass any pointers, obviously we're doing some overload do to stuff like that would be too much work. But otherwise if you actually are passed a new pointer you can do a check to see if it's the same or not. This is actually not required for to do the check because according the standard if you pass like raw pointer again that's already being managed it's undefined behavior but I'm just trying to be nice right like you want to be nice but what you what could be even nicer is to point out it's undefined behavior and you're being nice to your interviewer and your interviewer be like wow this guy really knows what they're talking about here. Here I'm also doing the copy and swap EDM. I create a temporary from new pointer and then I do a swap. It's so easy. It's like no brainer. You just need to remember like two lines basically. But let's look at the, you know, alternative, right? If you don't have, if you don't do copy swap, do a check and make sure you release what you own. And then if the new pointer is a new app, you shouldn't do anything to delay the control block allocation which way to call just by like do calling the constructor because the logic is already handled when we did the overload and then you know and now you finally go to allocate go to like you know initializing variables or resetting variables. I think it makes a lot of sense to start the count at one. And guess what? That's what GCC does as well. Here we go. And all of the implementations I've seen online, they all start the count at zero and then they manually increment by one after initializing the control block on the heap. Very disappointed uh to only resources. That's what I'm trying to change, right? Trying to spread the linings I have from, you know, reading the source code and asking really smart people. Another thing that's really interesting, I don't actually think you need to talk about it, but it's like I would, is how the SH pointer does not have a release function like the unique pointer does. The reason is that the unique pointer is like the only thing that owns the underlying resource. So when it needs to release and deallocate or like free the object or the resource, it just knows how to do it. I was deterministically because nobody else needs it. You can definitely delete it. But sh pointer is different. If you call release on a single sh pointer basically you're asking it to the underlying object and then what happens is that all the other sh pointers like they would be in like a indeterministic state like it's possible um you know the underlying resource is still there or it's possible not it can't just like only look at the uh you know the count and determine oh yeah count is like greater than zero so the object must still be alive so I can do stuff it has to like check the whether the object is actually released or not. So like basically it just doesn't make a lot of sense for share pointers. So we'll have that in the API in the standard. And then let's talk about the

### Segment 6 (25:00 - 30:00) [25:00]

swap function we talked about and this is the friend swap edium. You can look it up on stack overflow. It's basically an EDM. Uh you can you can not have it and you would you can still do the member swap and then still get the you know previous swap usage to work. But yeah, so the but this is like how you typically would want to do it. The friend swap EDM just like declare the member functions that take in like left and right and then just swap it in this way. All right, I already talked about the member variables and I'll try to release note that has to be item potent because the user can just call reset in a row multiple times, right? But if it's we don't own anything, we don't have to do anything. And then if we need to increment it. So decrement calling decrement on the atomic type would basically return the previous value. So it means like if before the decrement if the value is one then we should delete stuff because it just means now it's zero, right? It went from one to zero. Ignore this line. We'll talk about that in a bit. So we delete the underlying resources. when we delete the underlying. So we delete the resource and then because the nobody's the resource is gone like there's nothing to control. So we delete the control block as well and then at this point don't forget you need to basically set the variables to null. Okay. And uh in fact it's actually harder as you can see to write the mutex version compared to the atomic version because basically you have to lock the mutex in the control block first do the decrement do the if check and then you actually have to do the unlock because you if you do delete the control block at the end of the scope this fun function here, this unique log would go out of scope and it would try to unlock the mutex but you already deleted the control block that owns the mutex. So it's a very bad segmentation fault. So it's and then why is unlock safe here? It's the reason is that like at this point like you were the only one owning the you were the last one owning the resource basically or sharing the resource. So the question is like the only case that you obviously you can think of is what if somebody you know oh like right after we enter the if check made a copy of the myself like this sh instance and now the count is no longer zero. Oh, it's going to be really bad because we're going to delete everything. No, it won't happen because it's a very important distinction. Sh pointers thread safe point in terms of how they will point to the same underlying resource and control block. If you have multiple sh pointer instances, you can use them in different threads. However, the standard explicitly says that you cannot use the same instance of a shadow pointer in multiple threads at the same time. That does not need to be thread safe and that would not be thread safe, right? And that's exactly what's happening here. I'm just reiterate if you have the same shared pointer instance you're not supposed to like use the same share pointer instance in multiple threads like thread A and thread B shouldn't actually take like share pointer by reference and like do operations using it like they should take the share pointer by copies got it so in this case because nobody else can actually mess with me because I'm already in this current thread running at this location nobody else according to the standard would basically like mess with me. So it's actually safe because I happen to also be the last share sharer like owner of this resource. That's why we can delete and then unlock and delete without suffering through any concurrency issues. Moral of the story if you have shar pointer don't use mutex use atomics. All right. um copy from right we obviously it's sometimes it's just so weird like people who are doing like share pointer exercises like with me right and then I asked them yes like at the end of the implementation like so you never manipulated your accounts you never added to it you never decremented it like I'm like that's is just like but yeah it's just like remember to incorrect count when you're copying it, right? And then it copy over the resources and note how it also handles the case

### Segment 7 (30:00 - 35:00) [30:00]

where other has like no values. You would only skip the increment, but you still copy over the no values. Make sense? And that's the same for the move from look up exchange on CBP reference if you don't know what it does. And then there's also the SH pointer. Make sure the pointer. It's already pretty simple. register to use the perfect forwarding mechanism, but the syntax is pretty hard to remember. I personally I wouldn't care if you can't remember the syntax for perfect forwarding. Now we're going to talk about the advantages of mix shared right now. So there will be four advantages for mix shared. One is that you don't have to you basically forces you know to pass the same role pointer to multiple share pointers which is undefined behavior. And then the other thing is that it just ask like a random interviewee like what's the exact situation that make sure can help with the exceptions. So if like most people are not going to be able to answer at that field for example if you have something like this right okay paste it you have a function called process widget and then you if you just directly allocate if say if you want to pass it a share pointer right you would do sharepoint constructor and then here's a new and then if there's a second argument is like a new uh number like priority number you compute what could happen is that new widget is allocated. New vidget. Okay, it's done. Memory allocated and then you compute priorities called next. Yes, it does not have to go in the order of like strictly one parameter by parameter. The order is undefined in this case, right? So at this point if you allocation happened. Oh, we got exception and now what? And now basically we will like unroll the stack right and then not um the stack won't know that oh we allocated this it could this could have happened before the exception it could have happened after the exception in fact so like basically we have a memory leak how does share pointer help with this sorry with mix shirt you basically do this right so the mix shirt constructor would be like one single unit of operation either this happens before compute priority and then at the stack on row because the share pointer in the mix the shareepoint instance would call this would invoke the destructor would basically check the reference count and then do the deallocation. So that helps no matter a leak and then either or it happens after right and then you get an exception sh pointer is never it never did he allocation so yeah so either way was safe so that's how sh pointer can help and the other advantage is that you don't have to type out the templated type twice I don't really want to go into it you comment in the comment section if you're interested and maybe I I'll hop in and explain it for the sake of time but basically it's like sharepoint pointer does not have type deduction even after C++ 14 at 17 because the same as unique pointer like you want to know if due to various reasons like you can't do type deduction for example in the unique pointer case like you can't distinguish between whether you're doing whether you are actually pointing to T pointter or like an array of t but in the sh pointer case you would have think it's actually fine though so that's interesting right and then lastly memory allocation So it's actually less overhead for you to do shirt to do make sure compared to shirt because with make sure technically the implementation like the real implementation that I have done once would actually allocate the control block and the object in one like combined heap space. So it's basically the size of the control block plus object you're about to create. So in this case you only have to make one system call instead of like two separate system calls. Right? If you if you're doing like if doing new and then you pass it to share pointer essential new with the you know allocate once and then share pointer constructor would need to allocate the control block. So two system calls it's actually also more space like legitimately more space as well. Yeah, just less bookkeeping information potentially reducing the memory footprint. Um of because there are cases where you don't want to do use mixtured interviews won't expect you to really talk about it but just I just want to mention here for your knowledge why is like if you want to use like the brist initializer because the

### Segment 8 (35:00 - 40:00) [35:00]

smart points they use the paracis ones just what the standard decided to do to avoid like breaking old code and stuff and also the other thing is like if you have like a custom deleter or like the size is actually you override the operator new. So like the idle key the size is like not the size of t but rather like something else. It's super like uncommon case I say don't worry about it and then the last case is what we will talk about now weak pointer what are weak pointers are essentially imagine if you can use the color green you have a share pointer that points to something then you have something that share pointer that also points to the same underlying resource called the heap just going to draw And then if you just want like a reference to the object like expensive object but you don't actually care as much if it actually exists or not. For example, if you have a logger right or if I have like observer it has a logic to like basically do not do it like task if the object is free. In this case we can have a weak pointer. A weak pointer would basically say that I have a reference to either the control block or like yeah basically the control block and then I would only say that anyone who wants to use it would basically need to do a check first to see if the object is valid. What why do we have it? It's pretty helpful if say like you you want object to be deallocated if all the other resources references are gone like this one you I know I'm not important so I'm just going to be weak and ask for weak point pointer the other case is to help out with like this circular reference sometimes in design you have to do something like this so weak pointer make like you know instead of show pointer B you make a weak pointer B problem solved the destructor of SPA will see oh yes that you know nobody actually owns like a shared ownership to the underlying resource so I can deallocate safely yeah so that's the share pointer thing if there are still weak pointers around but there are no strong pointers around you can delete the underlying resource but you still need to keep the control broke around that keeps track of the weak pointers um because like the weak pointers would only know the whether the object is or not if by like checking by asking the control bro. So control bro cannot be deleted if you still have weak pointers. Right? So that actually that's actually the last thing I will make sure if because it does optimization of like putting the heap and the putting the object and the control block at the same location. You won't be able to just delete the underline resource without deleting the control block. Got it. It's like one single new one single pointer. Make sure it can't do it. As a result, if you expect to have sure the weak pointers to linger around and then you you feel like you might have memory pressure, um maybe just consider not using make shirt. Now if we really want to go deep into why make sure it cannot delete the things separately. Uh it basically has something to do with how like a technique if you think about it like if you just allocate you know exactly you know where the resource start with the resource end where the control block start ends. Um, but I think technically you could be smart about it and then like use maybe some really low-level system calls maybe potentially. But if you just do free like the free would want to like go back to your pointer to check how many bytes are actually in this region because that's how free figures out how much to delete or deallocate, right? So like you only have a single number, you don't have two numbers. So you can't call like delete twice in that case. But yeah, that's the mixture. I just want to tell you guys short pointers is actually still pretty common, but it's very tricky to get right. You see how we actually have like exclus like five if checks. But if you like don't if you don't do the copy and swap EDM, you have nine tracks. You think you can remember all the nine tracks when you do actually do your interview. Most people can't and in fact most of the interviewers they actually can't spot the bugs right away. So the so that's why we have test cases, right? We run the test cases and we just run the tests and see that everything passes. Good. Okay. That's

### Segment 9 (40:00 - 43:00) [40:00]

why we have tests. That's why the interviewers like me have a hard time like spotting all the bugs. Now you're looking back, right? You think in this implementation, can the control block actually own the underlying pointer for this particular implementation? Yes. But as I mentioned before, say if you do like a short pointer to rig pointer combining implementation in like maybe like two hour interview, three hour interview and then if you do like all the virtual pointer stuff, sorry like a virtual function invocation type rager detor then you can see why like you actually can output the raw pointer there. But also because like the there is a aliasing constructor. It's very stupid. It's really strange. Let me show you if you have like a strct right that actually only has a int. That's it. But like you can have a SH pointer that points to it that has like the strct type. But you can also have a SH pointer that says int. It basically like these two they can have same control block but they will have different pointers. So that's why like you actually put the pointers outside. I've never had to do this weird hack. Probably useful for some use cases. What's important on this kind of problem? I would say like it really it's a way to gouge if the interviewee is like up to date more slightly up to date with C++ like if they have done any outside programming or if like their course work actually is like slightly more like up to date at least like my school didn't teach me about smart pointers they kind mistakes like I said not actually manipulating the counts kind mistakes also like not deleting things right not using copy and swap EDM and they're messing up really badly when they try to implement something like reset, right? Or like implemented using mutex but forgetting like that you have to unlock in the middle of your scope. Terrifying stuff. What are the gates? I would say like most I would combine these two together. The to be in the top 10% you must obviously get the basic implementation right. have it be bug free to be the top 1% I would say you should have the concurrency stuff done really well as well use the you know u least strict memory orders I was planning to expand give you a few concrete cases for like why we have to do acquire release and why this can be relaxed but we don't have the time here so yeah so that's to be top one person need to be able to explain that and give concrete cases if you want like follow up on this I can do it just let me know in the comments ments and then to do to be the top 0. 1 performers you need to be able to talk about things like this right that I mentioned then you are like truly exceptional more like these these right like these this okay so that's everything hope you enjoy this walk through of the SharePoint solution extremely common problem you can definitely expect to be asked and check out the other videos I have made on my channel. Tech light and have a good rest of your day.
