K Closest Points to Origin (Javascript)
JavaScript Interview with a Microsoft engineer
Watch someone solve the k closest points problem in an interview with a Microsoft engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.
Interview Summary
Problem type
K closest points
Interview question
Find the K closest points to a vertex.
How would you modify the solution if the input was an infinite streak of points?
Interview Feedback
Feedback about Nimble Pumpkin (the interviewee)
- Advance this person to the next round? ✅ Yes
- How were their technical skills? 4/4
- How was their problem-solving ability? 3/4
- What about their communication ability? 4/4
You did really well. If you perform like this on your future interviews I have no doubt that you'll get hired at any of the jobs you're interviewing for. You have solid communication skills, you walked through the problem to make sure you understood what I was asking, went through most of the edge cases and made sure you tested it. The only thing I wish I saw (during part two where you kept track of only k elements) was to use a priority queue / max heap to keep track of just the k closest points in O(NlogK) time.
Overall, awesome job. I wish you the best of luck in your future interviews.
Feedback about Indelible Raven (the interviewer)
- Would you want to work with this person? ✅ Yes
- How excited would you be to work with them? 4/4
- How good were the questions? 4/4
- How helpful was your interviewer in guiding you to the solution(s)? 4/4
I liked the question, I thought it was a good way to quickly assess someone's thought process and see their coding abilities.
Overall, you did a good job giving direction when necessary.
I was a bit confused by the extension you asked to the question at the end. I was trying to ask some clarifying questions but overall it just wasn't super clear.
Interview Transcript
Nimble Pumpkin: Hello.
Indelible Raven: Hey, how's it going?
Nimble Pumpkin: It's going pretty good, how are you?
Indelible Raven: I'm doing all right. Been a bit sick, so I'm just getting over it. Hey, have you used this platform before?
Nimble Pumpkin: I have not, this is my first time, how about you?
Indelible Raven: I've used it quite a bit. So, let's see. I want to basically go over how it works quickly. You already set it to JavaScript, so good. So for me, it's the typical like 45-minute tech interview, but since intro is five minutes and Q&A at the end is five minutes, it's really 35 minutes of tech. So we'll get started with that, I see you're an experienced engineer, and then at the end I could give you your feedback and then you get the chance to ask me any questions on it or we could discuss it. Sound good?
Nimble Pumpkin: Cool, yeah sounds great.
Indelible Raven: So I'm gonna start you out with... well I don't know JavaScript so it's just kind of a guess. So I'm going to give you a point class, or you can write it as a 2D array or 1D array or whatever, so this would just be like an X and Y right. And then knowing that, I'm going to give you a list of points. So that will be something like (0,1) (1,-3) (2,4) (1,1) (13, 2). And I'm going to give you the point origin which will be let's say (1,1) and then K let's say for example 2. What I want you to do is find the K closest points around the origin based on distance and just return those points. So in this case it would be like (0, 1) and (1, 1).
Nimble Pumpkin: Okay. So find the... you give me K, and so I want to find the K closest points, got it. Okay cool, so I think the first thing is figuring out the distance formula, which the distance between two points I believe is (b2 - b1)^2 + (a2 - a1)^2 and then square root that term. I believe that's the formula for distance between two points, so we'll start with that, I think that's very important.
Indelible Raven: Yeah, that makes sense.
Nimble Pumpkin: Cool, so I guess there's edge cases as well which I can handle which are like there are no points, if no points array, I probably throw an error. So there's that. If there are less points than K, less than or equal to K, then I can just return points. That's kind of a quick exit early strategy...
Indelible Raven: Let's leave it at that.
Nimble Pumpkin: Cool okay. So I'm going to represent a point as kind of like you had it, a point is XY as an array, and the points is essentially an array of these, like what you had below. And origin is going to be one of those points as well. So I will first start I think at this point...
...
Indelible Raven: Yeah, it doesn't look like I can include just like any NPM library here, so it won't run, I mean I can try to just require it and see what happens. It might just blow up or maybe it'll work. Let's try it.
Nimble Pumpkin: Right, yeah. So I mean what is this if I'm going to be talking about error rates, like what is this a stream of that I'm looking at in order to determine like... you know when I'm stopping the stream, so you know what I've explained so far is like how I would do it from a technical standpoint, in order to basically say I can return the top K, great - so we've got that covered, so I guess this part of the question more pertains to what is the like context of the question, what are these points, and what is this stream and based on that I'll try to give you an answer.
Indelible Raven: Well the points is just the input stream of X Y coordinates, or X Y Z or whatever right? So what I was thinking here at least my perspective is hey what if we have some sort of like sliding window...
Nimble Pumpkin: Okay, so you're saying that that's the threshold essentially.
Indelible Raven: Yep, at least that's my thought process. So that was more of a discussion of seeing, hey what would you do in this case. So let's stop here, it's been roughly thirty five to forty minutes, and now I can give you your feedback, we could discuss it, you could ask me any questions you have if that sounds good.
Nimble Pumpkin: Yeah that sounds great, yeah I'd love feedback.
Indelible Raven: So I actually don't have anything negative to say to be honest, you did really well. Like you're fast, you came up with a good starting solution, ...
Nimble Pumpkin: Okay cool. What... I'm actually this just random out of curiosity question, but why are you interviewing on the platform, like is it for experience? I mean you have a lot of experience interviewing, why do you do it?
Indelible Raven: I actually started up a non-profit to help people get into tech ...
Nimble Pumpkin: Got it, cool. Alright well I think that's good, I appreciate the feedback a lot and I really appreciate you taking the time, it's very helpful for me and yeah.
Indelible Raven: So I'm going to write some written feedback. It's going to be along the same lines, ...