Pergunta de entrevista da empresa Google

Find max k elements among mostly sorted list.

Respostas da entrevista

Sigiloso

11 de dez. de 2013

Why would you sort the whole thing when you only need the top k? Any sort algorithm will take you at least nLog(n). But maybe, and probably, n>>k. You go over the original list O(n) and insert in a Heap, keeping only k elements. It's gonna be O(n Log(k)).

5

Sigiloso

3 de fev. de 2014

@santi, should use a min-heap. fill it up with first k elements, then if a new element is greater than heap min, pop the top and push new val. heap will have top k elements. this is o(n), since log(k) reduces to constant time.

4

Sigiloso

8 de out. de 2013

Insertion Sort

5

Sigiloso

15 de abr. de 2015

@Dave can you explain why you want to use a min-heap instead of a max-heap? I don't get this part.

Sigiloso

7 de mar. de 2020

Max-heap will have largest number at top - min heap has smallest number at top, so when you insert new number the smallest gets popped out, leaving all k max in heap

Sigiloso

15 de set. de 2013

Are you kidding? "Bubble sort k times" is going to be kn^2. Too expensive. Use a merge sort.

Sigiloso

23 de jul. de 2013

Bubble sort k times.

2

Sigiloso

21 de jul. de 2013

Pretty easy question.

3