RECENT INTERVIEW QUESTION: Find Sum-Pairs in an Array that add up to X (JS/Ruby Solutions)

Recently got this question in a technical interview for a junior dev position. It's a pretty straightforward algorithm... still fumbled through it a lil bit to start (nerves). They also asked me to do it in Javascript specifically, which I'm still getting comfortable with (Ruby was language #1 for me). Anyway, check it out below. I'll talk through what I like/dislike about the solution, and I'll include a Ruby solution at the end too.

Comments

  1. Yo! Did you think about sorted v. unsorted arrays? If the array is already sorted, you can move from the ends simultaneously to arrive at the middle. This would only require going through the array once, so it should be fast. I think even sorting the array first would still be a solid solution since sorting can be done relatively quickly. I'm rusty on algorithm speed, but I think you could get to n log n instead of n^2 doing that.

    ReplyDelete

Post a Comment

Popular posts from this blog

Starting something new