Pergunta de entrevista da empresa Google

How would you write a sort routine to ensure that identical elements in the input are maximally spread in the output?

Respostas da entrevista

Sigiloso

6 de jul. de 2010

To Jeff's question, I believe the answer is a. The best answer I can think of is to: 1) Sort the array - nlogn 2) Find the number of occurances the most used number is (x) - n 3) Create x stacks - n 4) Loop through the sorted array, adding each number to a different stack (i % x) - n 5) Empty each stack one at a time onto the array - n I think there's a card trick that does something similar ;) Time complexity: O(nlogn) Space complexity: O(n)

2

Sigiloso

21 de jun. de 2010

How "maximally spread" is defined? For instance, which of the bellow are maximally spread? a) "1,2,3,1,2,3" b) "1,2,3,3,2,1"

1

Sigiloso

18 de jan. de 2011

Joe, run your algorithm on 1 2 3 3 4 5 5 5 6. Your algorithm gives: 1 3 5 2 4 5 3 5 6. But which is the right one? maybe if you interchange 5 with 6 it would be better?