Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
4
of 4 vote

Using the given algorithm. The first merge takes 2n, the second 3n, the third 4n...

We are then given a summation 2n + 3n + 4n + 5n +6n + ... + kn

Which is equal to n*(2+3+4+...k)

Which is equal to n*(k*(k+1)/2 - 1)

Which implies a runtime of n*k^2

A faster solution would be to not merge successively. Merge all n until you get k/2 * 2n. Like merge sort, we merge evenly sized arrays at every step. This would yield a solution with the complexity of mergesort with K elements which is n*k log k. The amount of space is O(k*n)


Given K arrays with N elements each. First take the smallest value of each array and put it into the heap. Keep track of which array each value came from. Then pop the smallest value off the array and put this into the final array. Then insert another value from the array the pop came from. The runtime for this is also k*n*log k since we have to go through all k*n elements. Insertion into a k size heap is log k and pop is O(1).

- Some guy July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

ok... but which one is right " θ(nlog(k)) θ(nk2) θ(n2k) θ(nk) " ....???

- mohammad.n.aabed July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its θ(nk2) assuming you mean θ(nk^2)

- Some guy July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

yup that's what i mean.... thank you very much...:)

- mohammad.n.aabed July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The first answer is (k^2)*n
The second one is knlogk.
Build a Min heap whose size is K. Traverse every element of k arrays, whose size is Kn.
Insert an element into a heap takes O(logk)
So the total time is kn(logk)

- Chris July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A different approach:
**Benefit: We do not need to rearrange resultant array again and again as we do in conventional way merging first 2 then 3rd then fourth.... and so on....

-Have k integer index to traverse K sorted arrays
[index1,index2,index3,index4.......indexk]

-Initiate all with first element of respective sorted arrays.
[index1=array1's first index ,index2=array2's first index.......indexk=arrayk's first index ]

-Now compare elements from all traversing indexes [index1,index2,index3,index4.......indexk] in respective array.

-Which ever element is smallest.... copy that element to new mergeSortedArray[] and increase the traversing index by 1.

-keep repeating above steps unless all the arrays are not traversed.

- PKT July 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

While this result would work, its best to use a priority queue/heap to implement the comparison between K elements. Otherwise, you have to go through K*N elements for each looking through K elements to find the smallest yielding an O(N*K^2) solution. A heap will reduce this to O(N*K lg K).

- Some guy July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

that's great some guy.... +1

- PKT July 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use heap to perform k-way merge sort
Algo
1.create a min heap of first element of all the k array(time klogk)
2.remove the min element and enter the element of the same array to which min element belonged.
now repeat step 2
time-nklogk

- blackfever July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

d

- Anonymous July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure why you are using a heap? Is this really needed?
What about if you do:

1. while there is more elements in any of the k arrays -> O(nk)
	2. iterate over the first element of each k arrays and select the minimum -> O(k)
		3. remove this value from the array and put it in the result array -> O(1)

At the end it would be O(nk^2) right? The good news is that you don't need the extra heap

- gonandrap November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

ok... but which one is right " θ(nlog(k)) θ(nk2) θ(n2k) θ(nk) " ....???

- mohammad.n.aabed July 21, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More