Subarray Sum Equals K (Javascript)
Subarray Sum Equals K (Javascript)
Watch someone try to solve the Subarray Sum Equals K problem in Javascript with a Microsoft engineer. Explore this problem and more in the world's largest library of interview replays.
JavaScript interview with a Microsoft engineer: Subarray sum equals K
Interview Summary
Problem type
Subarray sum equals K
Interview question
Given an array and a target, return the total number of continuous subarrays whose sum equals the target.
Read more about the questions:
Interview Feedback
Feedback about Winter Squirrel (the interviewee)
Advance this person to the next round?
Yes
How were their technical skills?
3/4
How was their problem-solving ability?
3/4
What about their communication ability?
4/4
He did not right away go into solving the problem; he discussed the brute force initially and then worked through the optimal solution. He was good at taking hints and working through it. I would definitely pass him for the phone interview and love to have him as a colleague.
Feedback about Magnetic Rainbow (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?
1/4
How helpful was your interviewer in guiding you to the solution(s)?
4/4
I like that I have never seen this question (or variants) before. This let me have practice with an interview in which I struggle a bit. The only note I have is that other interviewers had me step through my code with examples to catch bugs or incorrect code. I've noticed that you told me right away as soon as I wrote something incorrect. I'm not sure if one method is better than the other, but it's something different that I noticed.
Interview Transcript
Magnetic Rainbow: So which language are you familiar with?
Winter Squirrel: JavaScript.
Magnetic Rainbow: Okay. Sure. We can dive into the question if you're fine with it?
Winter Squirrel: Yeah, sure.
Magnetic Rainbow: Okay. I'm copying the question here. So basically, you're given an input array, and an integer k. You need to find out how many contiguous subarrays in this array sum up to that given value, k.
Winter Squirrel: Okay, I'm just going to be quiet for a second while I read this. Okay. So, I'm just going to write down some assumptions, and then you let me know if they're okay. So I assume that if the input array is empty, then we're just going to return zero.
Magnetic Rainbow: Yep.
Winter Squirrel: Okay. And if we don't find any, then we also return zero.
Magnetic Rainbow: Yep.
Winter Squirrel: Okay. Um, I'm just going to say out loud what I think a brute-force solution here is, and then I think we could do better. So after I say it, please let me know if you'd like me to code it up, or move on to the better solution.
Magnetic Rainbow: Sure
Winter Squirrel: So since we need to find subarrays, the brute-force solution is going to be if we do a nested for loop to check every single possible subarray. The running time would be O(n^2), where n is the length of the array.
Magnetic Rainbow: Right.
Winter Squirrel: And the space would be just constant since we only need to keep track of the number of continuous subarrays we've seen. Would you like me to code that up? Or should I move on to trying to find a better solution?
Magnetic Rainbow: Yeah, I think we can do better.
... [Truncated for Brevity] ...
Magnetic Rainbow: Yeah. Yeah, so that’s cleaner, but our space and time stay the same.
Winter Squirrel: Yeah, that's true. You can actually try this sum in leetcode. Are you familiar with leetcode?
Magnetic Rainbow: I am.
... [Truncated for Brevity] ...
Winter Squirrel: Okay. Yeah. So I have a question for you as an interviewer.
Magnetic Rainbow: So, generally, interviews will know if the candidate has seen a problem before. If you already know the question, some suggest going ahead and mentioning it, while others recommend discussing your approach to show understanding.
Winter Squirrel: Hmm. Okay. Did I have any other questions?
Magnetic Rainbow: ... [End of Transcript Fragment] ...