Pergunta de entrevista da empresa HubSpot

The first question was on algorithms.

Resposta da entrevista

Sigiloso

12 de out. de 2016

Given a method which takes two unsorted arrays (A & B) and a number x, create an algorithm to merge and fetch the first x numbers of the merged array. Here there's a few ways to do it: -you can create a new array C of size k after checking the size of both arrays you're about to merge, then loop through each A & B and add them to the new new array. At the end a simple for loop will print out the first x values form the merged array. -proposed the solution of creating a Set of integers (mentioned that this won't contain duplicate elements) , loop through A & B add them to the Set and then at the end a simple for loop will print out the first x values. -proposed also the solution of creating and ArrayList of int and do the same as above, this will obliviously contain all duplicate elements...etc Then the interviewer asked what if A & B are sorted and now and I want to merge them but once the merge is complete, they should be sorted. I told him that I would merge them and then call the java.util.Arrays.sort(int[]) method that's sorts a specified array of ints into ascending numerical order. So I was asked what if I can't use the Arrays.sort method. Well, the only solution here is to be familiar with the Merge sort or Bubble sort algorithms unless you can come up with your own implementation. I pretty much hit the wall here because although I am familiar with sorting algorithms, how they work, i.e merge sort divide and concur with O(n log n) complexity as opposite to Bubble sort that is a O(n2) or a quick sort...etc I don't know the implementation by heart. So my answer here was, right in this situation I would merge the 2 sorted arrays and implement one of the the sort algorithms mentioned above, I told him about the Big O complexity...etc. The interviewer seemed to like my answer so I was asked to come up with my own sorting code, just give it a try at least...I eventually tried come up with some nested for loops...etc. "Lesson to learn form this - Learn at least a sorting algorithm my heart" Is not enough to name them, know how they work, Big O complexity - I think here is where I got a minus...and believe me I am pretty good a Java puzzles, palindromes etc.

3