HubSpot Interview Question

Code an algorithm to get the first n elements of a sorted array. The array is made by merging two arrays passed as arguments. Give the complexity of your solution.

Interview Answer

Anonymous

Dec 2, 2016

First easy, suboptimal solution in python: print sorted(arr1+arr2)[:n] complexity O(n logn) There is a faster implementation in O(n) if you use two pointers and select one element at a time then increment the pointer. Be careful about input sanitization, if len(arr1)+len(arr2)