Did my student pass my coding interview? (Very easy algorithm but tricky)

Phil interviews Minnie on finding the most frequent number in an array, with ties going to the larger number. Minnie plans a frequency counter but hits a snag: object keys become strings. Phil walks through the bug and shows an alternate approach using Object.values and Math.max.
Questions this video answers
Phil Choi
Minnie
Implement a function that returns the most frequent number in a non-empty array; if there's a tie, return the larger number.
No — the transcript states no empty arrays will be given.
October 23, 2024
Chapters
In this video, we'll continue the follow up of the behavioral video with the technical interview. The purpose of these is to see if the potential employee I'm trying to hire has the ability not only to use correct syntaxes and code, but to test their problem solving skills. So I have Mini here again today. We are going to test him to see if he would pass the technical interview that I used to give in my company previously where I was the tech lead. My name is Phil. I became a senior developer at the age of 30. If this video is of value to you, please share and like and comment your questions below as I read all of your comments.
Thank you for having me. Appreciate you.
Thank you for being here, Minnie. All right, let's get started. So let's just say, you know, I, we set up a time, I sent you an email and now we're joining on Zoom. Thank you for joining, Minnie. I know it was some weird times, but yes, thank you for making the time. We really like what we saw or I really liked what I saw from your interview, behavioral interview before you know, you're, you learn how, you know, you knew how to deep dive on the async a weight problems and all that kind of stuff. And now today we'll do a little bit of a technical interview. So how have you been doing since last time? Have you been coding? Have you been.
So yeah, I just, I'm really excited to be here. First of all, thank you for having me. You know, I don't really have like a plan B. I just wanted to really wait and see if this would, you know, if you guys would pick up the phone and call me because I really liked what I saw in this company and I felt like I would be a great asset to this company. So, yeah, I've been coding. You know, I have a few projects in the works now too as well and I'm excited to be here and I'm excited to show my skills and. Yeah, let's get to it.
That's awesome. So wait, what kind of projects have you been working on?
You know, just. I made, made a card game actually. Okay. It's kind of like a Magic the Gathering card game and I added authentication to it. There's a really cool channel. It's a left field channel. You know, I was able to look at the, you know, the form validation upload that they did recently and I was able to use react forms. Right.
Oh, that's awesome. React took form. That's a very, that's a very good library to make it Very simple to add validation. Yeah.
So yeah, well, you know I was able to like have a really good, you know, sign in, log in and yeah, I think it's gonna, I, I, it's something new that I added to my toolkit and I felt good about.
It, so it's awesome. Form validation, login. Are you using Firebase for your authentication system?
Yes. Firebase, yes. So that same first somehow, you know, I didn't have to look anywhere else that, that, you know, that YouTube channel had that already in store, you know, I just had to flip through it and yeah, just attended the free class there, you know, it was good. Okay.
Okay, awesome. So we'll just get started. I sent you a live share link.
Awesome, thank you.
Yeah, share your screen please. So we can kind of go through it.
I believe it's this one.
All right, so complete the method which returns number, which is the most frequent occurrence. So in the given input array and if there's a tie for the most frequent number, return the larger number or largest number. So, so just to note, it's always going to be an array that comes in and no empty arrays will be given to you. So I, I have three tests here. If you see it's 12, 10, 8, 12, 7, 6, 4, 10, 12 and 12 occurs three times. So you'd return 12 because it's the highest occur occurring. The next one is a little bit trickier. It's 12, 10, 8, 12, 7 6, 4, 10, 12, 10. So you can see as 12 occurs 3 times and 10 occurs 3 times and you have to return the higher frequency one. There are two tests inside of the highest rank test JS file. Can you open that one up? This one, right, yeah.
So.
Oh no, no. So these are the tests that are inside. So simple test is just the highest occurrence, it should be 12 and the next one should be also be 12. So let's get started. Mini, in this coding test of thing, when you are given a coding test, right? So this is a break from like the interview. It's very important to communicate the steps that you're going to do for solving the algorithm, right. Like during the job like syntax and all that kind of stuff, there are going to be much better programmers or like you know, whizzes or whatever that can solve a problem very easily if you explain to them exactly how you need it done, if that is what you need. Right. But like a company or whatever is trying to, it's trying to be a well oiled machine, right? So you're trying to be another cog in there, I guess. And you need to know how to communicate and break down a problem to really solve a problem. So more so than running the code or anything like that, it's about breaking it down to step one, two, three, four, and so on to kind of get the output that you need. So, yeah, let's first, you know, always have a plan and then code it the way you planned it. And then if you have a change in there, go back and change it. In your description of how you're going to do it, that's exactly how you should be doing it. All right, so yeah, let's get started.
Just kind of. So I will write down the steps and then code along each of the steps as I go. If we look here, we're given an array and we're supposed to find the highest frequency number. And if there's two numbers that occur the same amount of times, then you choose the higher one is what I got from this. So to start this, we need like a counter just to, you know, reiterate the problem. Basically, you're giving an array and then trying to find the number with the highest frequency. If there's two numbers with a tie, then you have to pick the higher, higher number. So to do that, I need a counter first. So I'm going to create an object. So create an object called num counter. And then inside this object, the key is going to be the number. So number would be like 12, 10, 8 or whatever. And then the value would be the count. And as I loop through the array, what I want to do is as you loop through the array, through the given array. So as you loop through the given array, update, count, or if a unique number is found, create a new, new key value pair. So I'm going to try to code that first and then move along to the next step. So. Yep.
So, well, you're going to create an object called num counter.
Yes.
And you're going to loop through and then if that key is inside of the object, you're going to add by one. Right?
Okay, so array I think I could use for each loop here. So for each number, what I want to do is I want to create a key called num and then it's going to have a value of either. If it's new, then it would be one. If it's not, then you would add one to the current one. So it would be first you have to see if the value exists this or you assign it zero and then you add one. So I think this will Work in looping through the array and finding all the. So it will. This essentially will keep track of how many times each number occurred in the array. So yeah, I'll just put it here. This is like counting the frequency of each number in the array. Each unique number in the array.
Okay.
Right. Yeah. Okay. So then we. We now have an object full of numbers and the number of times it's been counted. And what we need to do is loop through that object and find which one was the highest in terms of count frequency. So to do that, I'm actually going to create a new variable called the naming is being readable and highest. Let's just call it highest frequency. So create a variable called highest frequency. Yep. So I'll create that. Let highest frequency and it will start at zero. Because what I'm going to do is I'm going to use the for in loop to loop through the object that I created here. And it's going to loop through the key with the for in loop. And therefore I can compare its values at every iteration of the loop. So I'm going to loop through num counter with for in loop and update highest frequency. If num counter num is greater than highest frequency. So then what that would look like in code is for num in num counter compare. Right. So if highest frequency is less than or I can say, oh yeah, I'll just do this. Like I said in the pseudocode, num counter now is greater than highest frequency. Then highest frequency equal to num counter. So now I can find the highest frequency number, how many times it occurred the highest. And I also need a variable to keep track of what that number was. And if it duplicates, then I need another check for that as well. So I'll just do it. I guess this will be actually five. And I need to create a variable called so this one is a variable to keep track of track of highest, keep track of number of times number was number occurred, highest number of times a number occurred. And then this one would be highest number with highest frequency. So this naming is a little bit weird, but that's the best I can come up with now. So this keep track of the num with the highest frequency and I will update this variable if as I'm looping through you find a higher number with the same frequency. So I'll just write it here if number is greater than the frequency and then also if highest num with highest frequency is. Or actually I'll just do the same way if num if that num is greater than the highest num with highest frequency, then highest num typing this is tough. So I'm just going to actually change the name to result or something because that's what we're going to return anyways. I'll just do this. So if, if result is. Or if num is greater than result, then result is null. And then that's what we're returning at the end. So here, let's create the result. Let result. It will start at zero for the comparison and then at the same check here. I guess I'll do another if statement inside. If. If. If. What's. Whatever. Oh yeah. If num is greater than results, then results. This will work. Okay. At the end you return resulting. I think that should work.
Yeah, this is. This is very interesting. I like it. But I think just one. I think you need initializer on lane 30. I think it has to be constant or let num.
Oh, right here. Yes.
And then I think, just to be safe, I think the code will work. But just to be safe, when, when a key comes off of an object, it's a string, so you should probably Number.
Number like this.
Yeah. Just to be safe. You know what I mean?
Yes.
And yeah, it looks good to me. I mean, so first you have a num counter and then you have the. For each to get the counts. Then you have two trackers for the highest frequency and let result. Can you just take me through one iteration very fast?
Sure. Let's just take this for example. Okay. I'm just going to copy it down here for viewing purposes. So let's say this was the array that we're working, working with. Right. And so what happens is for each. Right. So it'll go through each number and it finds a 12. So the key of 12 will be made here. And then because it hasn't, it's a new number that isn't found in the num counter as a key. Then it will add plus one. So now basically what that would look like would be like 12, like this and then one, something like that. Right. And then it will go through all of this, the whole thing, array. So 10, same thing, 1, 8, 1. And it'll find 12 again. And since it exists then now the current number num counter of num is one, so it'll be one plus one. It'll become two. Right. And then it'll go through the motion again. It finds another 12, then it'll be three. Right. So it's basically essentially a counter that goes up by one each time. You find the same number, right? And so now you have that object made and it will look like 12, 3, 10, 2, 8, 1, 7, 1 6, 1, 4, 1. It'll be like key value, pair, like that. And then now use the for in loop to loop through the key of this array. So now you're looping through the object, which I'll just put it down. Actually, it's not hard to type. So it'll look like this. There's three of them and then there's two tens and then there's. The rest is like just, just one number, right? And then a seven. Just for visual purposes, I think it would be best, best for me to just type the type of cell six and then four. Right. So this is what the object currently looks like, right? And then it's going to loop through this key and then I can take the value of these keys and then compare it with the highest frequency. So because 3 was the high 3 is higher than 0 in the beginning, then now the highest frequency becomes 3 and then also checks like, oh, was this number the key like number of the key that becomes a number instead of a string? Was that greater than currently 0? Then okay, it's 3. So the result now is 3 currently. Okay, goes again 10. It fails to check here already, right? Because 2 is less than 3. So it just skips over 1 1, 1, 1, 1. And then because the result is 12 at the end of the for in loop, then the result become. It returns a 12. But let's say if this was three, or let's say like four was found like three times as well. Then what happens is it checks to see if current number of num or current results, which would be 12. Is this less than 4 here? Okay, so it's not. So the result would not update and the result will still be 12. It would return 12 as well.
Okay, what if the. What if four was 13 here, 13.
Then basically it would update this part here. That is a check that we haven't. And look that actually then I need to do all of this in one if statement. I believe I should. I need to do this. Okay, all right, you're right. That, that would. That would break this because the results not updated then.
Yes.
Right. Okay. Okay. All right, so then we just comment this out. I have to do a check here. If it was 13, we have to do this. And. And result is. So if the. If it's the highest, if num counter. If it is the highest 13, it's higher than the frequency 3, then it needs to update the result. Yeah. Or I think. Or num is higher than. Or result greater. Hold up. I'll just do it like this. And then this might not be the most ideal, but here, I'll do it. Number is greater than result or. Yeah. Highest count was greater than. Or not. Put this in here. I think, actually, I think this will work. I think. I'm not sure.
Let's just take it there. So right now, highest frequency, let's just say is 12. I mean, three and result is 12, right?
Yeah.
All right, so can we just. Let's just pseudocode this real quick. And then now on line 41, the number number counter at num that the value comes in at 13. It's higher than frequency. It's higher.
3.
Right. It came in here again. And then now if number num. So that's four is higher than result. That's untrue. But then we. It's the num counter. 13 is bigger than highest frequency after you've reassigned highest frequency.
Right.
See, I think this is.
Okay, it's not clean, but.
Well, I'm trying to navigate through. So it just feels like it bypasses, Right? Yeah, Right. Now, and then highest frequency gets to 3, and then it becomes highest frequency becomes 13 and result becomes a new one. Right. So only way it could. It could pass. This is the highest frequency, see, is bigger. Okay, I think that's good.
Can I do equals as well just to make it.
No, I don't think that's right.
Oh, yeah, it has to be like this. Okay. Yeah, I think this is fine. Wait, but if I do this right?
Yeah.
Then I don't think I need this line at all. Can I just do this?
Okay, let's think about this. Okay. Right now let's say Highest frequency is 12. I mean, highest frequency is 3 and result is 12. Right. And then we got to the iteration where we got to the iteration where the num comes in and it's 4 and the highest frequency is 13. Right?
Right.
So we go to the first check. Is is num. So num4, is it bigger than result?
No.
No. All right, but is num counter at num 13 bigger than highest frequency? Yes. Now highest frequency gets equal to 13, result gets equal to 13, but then.
It all becomes 4.
But then here, I mean, then at this point. Right. What if 14 comes in and it's at 13 as well?
Like after four?
Yeah, after four, let's just say.
Okay, so like 14 here becomes like 13 right here.
Yeah. Yeah.
Okay, so let's just Go through that part real quick. So num is currently. Result is currently 4 and highest frequencies is 13.
Yeah.
So number 14 is greater than 4, just bypasses automatically and then the highest frequency is still 13 and then the result would be.
All right. I mean, all right, what if we put 14 at 11? Let's just say.
Oh, here.
Yeah, okay.
Oh, yeah, you're right, that doesn't work. Yeah. So okay, I need a one more condition to make sure that both are okay. I think it's here, I need another condition here. So if 4, so I think this num is 14, this is fine. I think this is the most important. Check this part. This part is the most important. So I do I leave this alone, I need to modify this part because if this is highest, then it just should automatically update this part. This stays the same. This has to have like an end of something, some sort to make it limited. So if and num counter num is greater than the highest frequency, I think this will work.
That's. That is exactly your OR condition. No.
Yeah, it is. It is. Oh, oh, oh, oh, oh, oh, oh. If it's equal, right? Because if it's already higher, then it doesn't matter, but if it's the same, then then it should. Yeah, I think so. Yeah, I think so. This is correct.
Okay, let's take me through, take me through it. Right now it's 12 and 3, right? Let's just say it's 12 and 3.
Yes. So that's, it's 12, 12 and 3 and then it runs into this 4 and 13, right?
Right. So 4, right now number is 4, 4 and result is 13. I mean result right now is 12.
Yeah.
So that this automatically becomes false, right?
Yeah, this is false. And then. But this is true.
So this one passes in, right?
Yes.
And now highest frequency equals number, number, num counter num.
So now it's 4 and 13. Right now because this passed, this was false, but this passed. So it becomes like that and then 14 and 11. So this is false. But here, here because. Yes, the new number is greater than 4. Yes, because the num counter is, does not equal to the highest frequency, then it doesn't pass, so it doesn't update. But now here 14 and 13 because the highest frequency is same as the value of this number that we're currently on 13, this passes.
Okay, I think what you're trying to do, I think it works.
Yeah. Okay. I think so.
Let's go. Okay. Very good job, Minnie.
Thank you.
How I would kind of optimize your code a little bit. Right. Or how I would do it. Right. So here, just go through your steps here. I'm just going to command Z to where you were. Right. I would say, you know, whoops, do write some comments here. Be an array number of turns number larger highest recurrence.
All right.
And I just put like number one count of elements in array. Two, I would, I would then use object values and use math dot max to get highest number. And then three, I would probably use filter to get those, to get those values. And then 4, if 1 length after filter return that key. If else I would extract keys and run math max again. And I think this would, is how I would do it. But I, I feel like yours is very performant. Right. Like, but this is what makes sense in my mind. So I'll just, I'll code this out really fast. I'm going to take the accumulator and current and do what you did. That's very clever. And I'm going to plus one. So basically at this point we have the count, right?
Yeah.
And then we have the nums count. And then on here I would say object dot values on the nums count and I'd spread that into a math max like this and const max equals like this. And then I would want to do a filter. So maybe object dot entries on the nums count and dot filter. And now it's iterable, right? Because now it turns it into a key value array.
Like it.
So now I can do key and value. Whoops. I can do key and value destructure that and then I can take that one and then I can say val equals max and then at this point const filtered. And I think from here I can do if filtered dot length equals one, then I can, I can just return the key, right?
Yeah.
As zero. Right. And then if I don't, if it doesn't hit there, then I can just get the, I can get the keys now, right? And then, now I can just do something like filtered.map f and then the f I bring back zero, that's the key. And then I can run up, I can run return math max on that, which on these guys. And I think that return that would be done.
Yeah, yeah.
You understand?
Yeah. There's a lot of advanced techniques, dude.
But there's a lot of, there's a lot of loops in here. So that's why I said you're European.
More from Phil

day in the life of a software engineer | 5 hour productivity | building my dev agency 🔥

day in the life of a software engineer | 3 hour productivity | coding my 6-figure community startup

day in the life of a software engineer | 5 hour productivity | coding our sns startup app LFG

day in the life of a software engineer | 5 hour productivity | creating apps that makes big money






