The MOST powerful way to solve coding algorithms (any LeetCode problems involving arrays)

YouTube tutorial by Phil Choi demonstrating how to use JavaScript's Array.prototype.reduce to solve algorithmic interview problems (Advent of Code examples). Phil explains reduce's accumulator/current pattern, shows parsing input lines into left/right lists, computes checksum (difference between minima) and a frequency-based similarity score, and argues reduce can replace map/filter/forEach in many cases. Informal, example-driven, with runtime demos and debugging.
Chapters
The job market is slowly opening up again and people in my discord are getting interviews and jobs left and right. Anything to do with LLM, chatbots and blockchain are coming back up. With that being said, no one is even going to give you a chance unless you get through those dreadful technical interviews. It's something we all got to face as a junior developer today I wanted to show you a very useful array method that you can fall back to when in doubt. The reduce method. In this video I will demo this method using the algorithm challenge from the Advent of Code. If you guys don't know what Advent of Code is, it's a series of Christmas themed algorithm challenges that get quite difficult. I do this together with my community and we have our own leaderboard as well. So come by and join and Discord join the discord to join in on the education. So my name is Phil. I became a senior developer after the age of 30. If this video becomes of use to you, please like share and comment. An experience you went through where the interviewer gave you an insanely tough algorithm for a technical interview. Let's share and give each other encouraging words in the comments below. Help us reach 10k SOL to for the end of the year. So here I guess we'll just do the Advent of Code day one. I guess it's a pretty simple one and I think you can finish in like maybe like one line or something like that. So here we are. Whoa, that's really small. Anyways, like with these technical interviews and stuff, honestly like I've been kind of like thinking about it and I feel like, I don't know if you guys watch football, but I watch a lot of NFL football and play a lot of Madden. But there's like a 40 yard dash in the NFL draft in the combine and like how fast you can run doesn't really like translate into the sport, but I feel like it like makes it or breaks it if you can play at that level. And I think a technical interview is a good analogy with like a 40 yard dash. If you can get through an algorithm you can find you can be like useful anywhere. So here, that's just my two cents that I was like thinking about recently. I thought it was quite interesting because it's not that important, but it's. It could be interesting. So let me see. I'm way behind on Advent of Code this year. Here's day one. All right, let me just open this code. So let me just open up Advent of Code as well. So basically we have two Sides and we have three four, two one three, three. We have to basically get the smallest number on this side and the smallest number on this side, and then we need to get the difference between them. So we can use like math absolute and kind of get that done. So right now, if I run part one, let me see node solution JS, it's going to give us the 344-325-133933. So let's see how we can do this here. Let's do a reduce here. So reduce is really cool. If we open up the documentation, it's going to actually say reduce mdn, the reduced method of an array instance executed super supplied reducer. So I feel like the reducer, the reduce basically callback function on each element of the array in order passing the return value from the calculation on the preceding element. The final result of the running of the reducer across elements of the array, it's a single value. So you kind of can reduce it into some single piece of data. You can start off with a number, an array or an object, and that array can turn into a different piece of data. So a very common thing is kind of adding, adding up an array, like finding the sum of all the arrays. So you have an initial value. So that's going to be. So the syntax is you have your. You have an array and then you have the reduce method off of that array prototype. And then you have callback that you call and then you have. And then you pass in the initial value of what it starts at. So you can always think about accumulator and current. And the accumulator will be at the first instance at this accumulator will be. So let's just think about what this reduce is doing at the first instance when index is zero, right? Accumulator is zero. And the current value at that first index zero is going to be one. Whoa. And then basically you're saying, hey, I want to return the accumulator plus one. So now the next time it runs, the accumulator is going to be 1, because 0 plus 1 was 1 and it's going to. And the next one is the current value is 2 at that next index. So now you return 1 plus 2, which is going to be 3, and that accumulator gets passed on to 3. And then it's 3 plus 3, which is going to be 6 and that accumulator. Now the current value at the last index will be 4. So it's 6 plus 4. And then we get our expected output of 10. And I always like to go through like a. Like if I'm working with a for loop or something. Always think about zero index or something and kind of like hard code a little bit like in comments to kind of figure it out. But reduce is a callback function and then you have an initial value. And so a lot of like there's a lot of. There's a lot of documentation here on how it works. It gives you back a lot of other stuff. So this is kind of like what we just did here. I'm trying to see where it goes back something else. But I feel like any array method like map for each everything it can be actually replace with a reduce function. So let's kind of see how that can happen. All right, so let's open up the code. So we have contents. We basically have an array 344-325-133933. So we actually need to see the differences from all of their. So we can actually have a callback here. Accumulator. Current accumulator. And we'll call it current. And then maybe we'll start off at zero. And so we basically need to take a left value. So we'll say left and right. And I'm thinking can we all do. Can we do it all in one? Let me think. And we'll take it from the current. Current. Maybe we can't do it yet. Here, let me just try this out. I don't think we can do it yet. We need to split up the left and the right first. So we need to do something like. I guess we could do something like this. Then we can do on current and then we can do a dot split at the three. The three. The three spaces. And then I'll just do a dot map at the S dot trim. Whoops. S trim cast to a number. There we go. And then we can just do accumulator at left and then dot push left. That's really bad naming. But dot right dot push at right. So we have our starting object and then we have what we're going to do at every single index. So right now at 3 and 4 we're going to split it and then map the string that's a number and cast it to a number. Then trim off any white space if there is any. So maybe that's our first reduce. That we can do. So let's console log content here. Whoops, got an error. Let's see here. Cannot read properties. Undefined reading left. Oh, we never returned, sorry. Then we just return the accumulator. Okay, now There we go. So now we have the left on one side and we have the right on one side. So if that's correct, we'll go back. Let's look at our log here. Let's look at the sample TSX 3, 4, 2 1, 3 and 3, 3, 4213 and 3. Oh, 3, 4, 2, 1, 3 and 3 on the left and then 4, 3, 5, 3, 9 and 3 on the right side. So let's kind of, let's think about how to do this next one. I think I could do a sort or we can do something a little bit more fun with math dot min maybe. Let me think. We can do while content left and length and contents taunts right dot dot length. While they both have value because they're going to be the same length, we can do like const min on left min L equals math min at the content left and then min R would be the math min at contents write. And then we can do something like. And then from there we can do content and then we can do. Then we need some kind of total. Let total equals 0 total plus equals on plus equals on the min l math abs on min l minus minr and and then we need to remove that min. So we can just say content left dot splice@contents.left.index of min L at 1. And then we can do the same thing on the right side as well. Admin R. We can do it without sorting it. I guess that's console.log total equals total. So we get a total of 11. So that was kind of. I guess that's, that's what they wanted. Let me see here. We're gonna try to do it with this one. Let's get this guy's puzzle. We should have gotten 11. That's cool. Get your puzzle input. Let's get this one. Let's see if we can. It'll come out with the right one here. So let's do the input. Txt here. Cool. Let's actually see if this input input is the same as. So I see on the first one, I see 27484. That's the one that I got with my 555634. Oh, it's not the same. So they're gonna be different numbers. So here, let's kind of just go through this real quick. One second. Did the input. Txt. Let's just make sure it's correct. Run it. I got this number 274-2-7742 123. Let's put this in submit. Oh, that's the right answer. You got one gold star closer to finding the chief historian. Then we get a part two. So there's always two parts in every day. The next one is the similarity score. You gotta get frequencies. So like instead of adding the differences, the first number in the left list is 3. You gotta find how many times it appears on the right side. So the algorithm is pretty similar. We can try that one too. It's already a 20 minute video mini. I know you're there. I think we cut it here, but I think. Can you just explain why reduces so good, like versatile. So reducer is really good because you know, you can, you can. I really like it because you can start it out with any variable because usually you're like putting this in like another variable, right? So basically you're like doing something like this and you're doing like lead left and right equal like this. And then instead of the reduce, maybe you're doing some kind of foreach. Right. So let me just show you. You can do some kind of content foreach. And then on here you just take the element, element that comes and then you say you kind of take the same logic I guess here, but then you would put it inside of this left and right thing instead. So left and right dot left naming the variable and the key is really bad. But it's a video from previous, but it gets me a little bit confused. But left and then you'd say dot push on left and left and right dot right dot push on right or something. And it would be kind of the same code at this point, right? Yeah, I'm sure. I think it would work the same. Oh, I got an error. Oh, I got a red line somewhere. Let's fix that. I don't know where that is. This one. Where did I get the red line from? Oh wait, when did I ever get the rid of the function Part one. There you go. And then there we go. Then it would kind of be the same. Oh, well, current is not defined. Where's current coming from? Oh no. Oh, current dot split for each. You put current in the foreach tool. Oh, you just call this L And then. Wait, wait, wait, wait, wait. We're gonna get a problem here. That's why reducer is good, right? Yeah, it is. But then we'd have to change all this to yeah. Crap. There you go. I think you should run this in. Wow. Getting a lot of errors, bro. Current is not defined again. What if I don't try to rerun your code? Oh my God. There. Okay, so see how we got the same number? Yeah, I don't like, I like. I'd rather put it in one line, but I just feel like, like, you know, let's just. For example. For example, right? Let's just say like you have like a function and add two or something, right? And then it takes an array and then it just. You can just return array map at that L L +2, I guess. And then you could do something like add const 1st equals add to on this array, let's say, right? So we expect 3, 4, 5, 6, 7. Right? So we get the 3, 4, 5, 6,. But with that. And then. So, so with that same thing, you could actually do it in reduce too, right? You could do return array. Reduce and you take the accumulator and the current and then you can say return at and spread the accumulator to the current element plus two. And you started off an array. All right? So then you kind of get the same. Same result as well. And you can. There's like many other instances as well. Let's say like any. Anything for each filter. All of them, they're. They're gonna be the same, right? And you can do it with reduce, like filter. Let's just say we have a function here. Function filter out twos, right? And then you have return array dot filter. If the L does. L doesn't equal 2, right? And then we can just say const F equals filter out two 1, 2, 3, 4, 5. And we should basically, we expect. We expect that it says 1, 2, 3, 1, 3, 4, 5 now, right? So it says 1, 3, 4, 5. And then we can kind of change that to a reduce by saying array array dot array Wait. I'm thinking array array dot reduce. Forgetting my code now. And you have an accumulator and you have a current, and then you can kind of say you're going to return the accumulator. And then you can say if only. If the current doesn't equal two, you return curr. And you start off an array and then it should be the same. Whoa, it's two. Sorry, hold on. I try to do. I try to be sneaky and do it in one line. Let's do it like this. If. So we do. If current doesn't equal to, we will return the spread accumulator at current and else we can just return accumulator and now we get the same result, right? So reduce. You can kind of think about it like it could just replace every single array method because it's so powerful with what it can do. Like, I, for me, with reduce. A lot of times what I do is just say like. I know, like the second part, I feel like, to. The algorithm could be like a really good place to use it. But let's just say you have. Let's just say. Let's just say we have function part two, right? Yeah. And then we have const. Content equals utils.open utils.open file at dot slash sample txt and then let's just say that we want to get const. Write count or something like that. Right. And then actually, right here, maybe I'll just copy paste that code that we made earlier. This one. So, yeah, go down here.
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






